Node-jsonwebtoken: version new jsonwebtoken error

Created on 26 Sep 2017  路  7Comments  路  Source: auth0/node-jsonwebtoken

I use the new version with the error occurred "jsonwebtoken": "^8.0.1",.

I use the old version the bug has been removed "jsonwebtoken": "^7.4.3".

MESSAGE ERROR ==>

      var token;
                        try {
                            token = jwt.sign(doc, jwtSecret, { expiresIn: '10h' });
                        } catch (error) {
                          console.log("ERROR==================>"+ JSON.stringify(error));     
                        }


events.js:160
throw er; // Unhandled 'error' event
^

Error: Expected object
at validate (E:\App\code\node_modules\jsonwebtoken\sign.js:34:11)
at Object.module.exports [as sign] (E:\App\code\node_modules\jsonwebtoken\sign.js:100:7)
at E:\App\code\dist\app.js:402:37
at Query. (E:\App\code\node_modules\mongoose\lib\model.js:3841:16)
at E:\App\code\node_modules\kareem\index.js:273:21
at E:\App\code\node_modules\kareem\index.js:131:16
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Most helpful comment

You can try .toObject() -> jwt.sign(doc.toObject(), jwtSecret);

All 7 comments

We have changed the validators used for the sign method. Take a look to the first item in the migration guide: https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v7-to-v8

We use lodash now, take a look to this function: https://lodash.com/docs/4.17.4#isPlainObject

Anyway I think we should communicate better that error, it could say something like "expected payload object to be a plain object" or similar.

Can you share the content of doc?

@ziluvatar : I've added a PR to change the wording.

PR was merged. It's on v8.1.0

Can this be fixed by simply adding .toJSON()?

You can try .toObject() -> jwt.sign(doc.toObject(), jwtSecret);

@kedrovski or @UNADYNE solution works fine but i suggest you to use: JSON.parse(JSON.stringify(doc)) if your doc object comes from diferent sources, for example:

You could have to pass diferent "doc" objects, one returned from mongoose and another defined by you like this:

doc = { name: 'foo', description: 'bar' }

If you use .toJSON() or .toObject() You will get a "doc.toObject/doc.toJSON is not a function" error with the doc you defined manually.

wont there be a performance issue using json stringify with json parse ? but yeah toJSON() or toObject() both comes as undefined on the object

Was this page helpful?
0 / 5 - 0 ratings