I get this error when I run and can not seem to resolve it.
TypeError: data[key].trim is not a function
The function is this one
JavaScript
x
26
26
1
const generateSignature = (data, passPhrase = null) => {
2
// Create parameter string
3
let pfOutput = "";
4
for (let key in data) {
5
if (data.hasOwnProperty(key)) {
6
if (data[key] !== "") {
7
pfOutput += `${key}=${encodeURIComponent(data[key].trim()).replace(
8
/%20/g,
9
" + "
10
)}&`;
11
}
12
}
13
}
14
15
// Remove last ampersand
16
let getString = pfOutput.slice(0, -1);
17
if (passPhrase !== null) {
18
getString += `&passphrase=${encodeURIComponent(passPhrase.trim()).replace(
19
/%20/g,
20
"+"
21
)}`;
22
}
23
24
return crypto.createHash("md5").update(getString).digest("hex");
25
};
26
I am not sure how to solve this problem
Advertisement
Answer
I think there is a possibility what data[key]
isn’t a string type.
try this:
Change if(data[key] !== "")
to if(typeof data[key] === "string")