简体   繁体   中英

jwt is not defined even after i imported it with require()

I have follow problem:

In my controller createPost i need to use the library JWT but it seems to be undefined and i dont know why

   const jwt = require("jsonwebtoken");
   const atob = require("atob");
   import Post from "../models/post";
   export default async function(req, res) {
     const token = req.body.token;
     if (!token) {
       return res.status(403).send({
         message: "Token ungültig, bitte melde dich ab und wieder an."
       });
     }
     jwt.verify(req.body.token, "key", function(err) {
       if (err) {
         res.status(403).send(false);
         return null;
       }
     });

as you can see i have importet it up here with const jwt = require("jsonwebtoken"); but its still undefined at line 11.

jwt.verify(req.body.token, "myKey", function(err) {

What i am doing wrong here?

Here is also my error message:

ERROR jwt is not defined
19:50:35

at default (api\\controllers\\createPost.js:11:3) at Layer.handle [as handle_request] (node_modules\\express\\lib\\router\\layer.js:95:5)
at next (node_modules\\express\\lib\\router\\route.js:137:13) at Route.dispatch (node_modules\\express\\lib\\router\\route.js:112:3) at Layer.handle [as handle_request] (node_modules\\express\\lib\\router\\layer.js:95:5) at node_modules\\express\\lib\\router\\index.js:281:22 at Function.process_params (node_modules\\express\\lib\\router\\index.js:335:12) at next (node_modules\\express\\lib\\router\\index.js:275:10) at Function.handle (node_modules\\express\\lib\\router\\index.js:174:3) at router (node_modules\\express\\lib\\router\\index.js:47:12) at Layer.handle [as handle_request] (node_modules\\express\\lib\\router\\layer.js:95:5) at trim_prefix (node_modules\\express\\lib\\router\\index.js:317:13) at node_modules\\express\\lib\\router\\index.js:284:7 at Function.process_params (node_modules\\express\\lib\\router\\index.js:335:12) at next (node_modules\\express\\lib\\router\\index.js:275:10) at node_modules\\body-parser\\lib\\read.js:130:5 at invokeCallback (node_modules\\raw-body\\index.js:224:16) at done (node_modules\\raw-body\\index.js: 213:7) at IncomingMessage.onEnd (node_modules\\raw-body\\index.js:273:7) at IncomingMessage.emit (events.js:203:15) at IncomingMessage.EventEmitter.emit (domain.js:448:20) at endReadableNT (_stream_readable.js:1145:12)

运行命令

npm install jsonwebtoken

Did you tried with var ?

var jwt = require("jsonwebtoken");

Just in case...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM