Skip to content
Advertisement

Nodejs trim() is not a function

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

const generateSignature = (data, passPhrase = null) => {
  // Create parameter string
  let pfOutput = "";
  for (let key in data) {
    if (data.hasOwnProperty(key)) {
      if (data[key] !== "") {
        pfOutput += `${key}=${encodeURIComponent(data[key].trim()).replace(
          /%20/g,
          " + "
        )}&`;
      }
    }
  }

  // Remove last ampersand
  let getString = pfOutput.slice(0, -1);
  if (passPhrase !== null) {
    getString += `&passphrase=${encodeURIComponent(passPhrase.trim()).replace(
      /%20/g,
      "+"
    )}`;
  }

  return crypto.createHash("md5").update(getString).digest("hex");
};

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")

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement