I need some help with jwt - expiresIn syntax

 Hi there,

Can anyone please help me out here?
I am writing this syntax:

   const jwt = require("jsonwebtoken");

const generateToken = (data) => {
  return new Promise((ok, not) => {
    jwt.sign(data, process.env.TOKENKEY, { expiresIn: "7d" }, (err, token) => {
      if (err) not(err);
      else ok(token);
    });
  });
};

and calling this function in a try-catch syntax like this:

let data = await JWT.generateToken(req.body.us);
    console.log('token ', data);

but I keep getting the following error:
“err Error: invalid expiresIn option for string payload
at Object.module.exports [as sign] (C:\fullstack\node.js\final project\Node_Project\node_modules\jsonwebtoken\sign.js:128:22)”

*** without the “expiresIn”, it works perfectly.

Hi @tbookra,

Welcome to the Community!

It looks like data you are passing is a string, and you can only set expiresIn when that payload is an object.

Can you try that?