Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version new jsonwebtoken error #403

Closed
thandonguocmo2020 opened this issue Sep 26, 2017 · 7 comments
Closed

version new jsonwebtoken error #403

thandonguocmo2020 opened this issue Sep 26, 2017 · 7 comments

Comments

@thandonguocmo2020
Copy link

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)

@ziluvatar
Copy link
Contributor

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?

joovel pushed a commit to joovel/node-jsonwebtoken that referenced this issue Sep 27, 2017
@joovel
Copy link
Contributor

joovel commented Sep 27, 2017

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

joovel pushed a commit to joovel/node-jsonwebtoken that referenced this issue Sep 29, 2017
ziluvatar pushed a commit that referenced this issue Oct 9, 2017
* #403: Clarify error wording.

* #403: Improve wording for payload vs options
@ziluvatar
Copy link
Contributor

PR was merged. It's on v8.1.0

@UNADYNE
Copy link

UNADYNE commented Dec 20, 2017

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

@kedrovski
Copy link

kedrovski commented Jan 9, 2018

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

@NodeJoSe
Copy link

NodeJoSe commented Jan 15, 2018

@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.

@sadi304
Copy link

sadi304 commented Jul 21, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants