【问题标题】:Expected "payload" to be a plain object : MEAN预期“有效负载”是一个普通对象:MEAN
【发布时间】:2019-03-17 19:12:21
【问题描述】:

这是我来自路由文件(users.js)的代码

User.findOne({linkedin_id: req.body.linkedin_id}, function(err, linkedinUser) {
    if(err) {
      console.log('err in finding linkedin user '+err);
    }
    // if user exits
    else if(linkedinUser) {
      console.log('user exist');
      const token = jwt.sign(linkedinUser, config.secret, {expiresIn: 604800});
      res.json({success: true, token: 'JWT '+token, user: {
          id: linkedinUser._id,
          linkedin_id: linkedinUser.linkedin_id,
          name: linkedinUser.name,
          username: linkedinUser.username,
          email: linkedinUser.email,
          lkprofilePic: linkedinUser.profilePic
        }, msg: 'user exits'
      });
    }
    // if user doesn't exist
    else {
      User.create({
        linkedin_id: req.body.linkedin_id,
        name: req.body.name,
        username: req.body.username,
        email: req.body.email,
        lkprofilePic: req.body.lkprofilePic
      }, function(err, result) {
        if(err) {
          res.json({success: false, msg: 'failed to add'})
          console.log('error in adding the data '+err);
        }
        else if(result) {
          const token = jwt.sign(linkedinUser,config.secret,{ expiresIn: 604800 });
          res.json({success: true, token: 'JWT '+token, user: {
            id: result._id,
            linkedin_id: result.linkedin_id,
            name: result.name,
            username: result.username,
            email: result.email,
            lkprofilePic: result.profilePic
          }, msg: 'User added '  });
        }
      });
    }
  });

来自config -> secret

module.exports = {
    secret: 'bigfish'
  }

这是我在 nodejs 控制台中遇到的错误

接收linkedin数据 D:\product\project-1\node_modules\mongodb\lib\utils.js:132 抛出错误; ^

错误:预期的“有效负载”是一个普通对象。 验证时 (D:\product\project-1\node_modules\jsonwebtoken\sign.js:34:11) 在 validatePayload (D:\product\project-1\node_modules\jsonwebtoken\sign.js:56:10) 在 Object.module.exports [作为符号] (D:\product\project-1\node_modules\jsonwebtoken\sign.js:108:7) 在 D:\product\project-1\routes\users.js:415:29 在功能。 (D:\product\project-1\node_modules\mongoose\lib\model.js:4177:16) 并行(D:\product\project-1\node_modules\mongoose\lib\model.js:2230:12) 在 D:\product\project-1\node_modules\mongoose\node_modules\async\internal\parallel.js:35:9 在 D:\product\project-1\node_modules\mongoose\node_modules\async\internal\once.js:12:16 在 iteratorCallback (D:\product\project-1\node_modules\mongoose\node_modules\async\eachOf.js:52:13) 在 D:\product\project-1\node_modules\mongoose\node_modules\async\internal\onlyOnce.js:12:16 在 D:\product\project-1\node_modules\mongoose\node_modules\async\internal\parallel.js:32:13 在应用时(D:\product\project-1\node_modules\lodash_apply.js:15:25) 在 D:\product\project-1\node_modules\lodash_overRest.js:32:12 在 callbackWrapper (D:\product\project-1\node_modules\mongoose\lib\model.js:2199:11) 在 D:\product\project-1\node_modules\mongoose\lib\model.js:4177:16 在 model.$__save.error (D:\product\project-1\node_modules\mongoose\lib\model.js:359:7)

但是数据被保存在数据库中并且没有返回

res.json({success: true, token: 'JWT '+token, user: {
            id: result._id,
            linkedin_id: result.linkedin_id,
            name: result.name,
            username: result.username,
            email: result.email,
            lkprofilePic: result.profilePic
          }, msg: 'User added '  });

【问题讨论】:

    标签: javascript node.js angular express mean-stack


    【解决方案1】:

    问题在于您签署令牌的方式

    您使用的用户是 mongoose 的返回用户,因此您需要使用 YOUR_USER.toJSON。如果用户不是来自猫鼬,请改用 JSON.stringify(YOUR_USER)

    将您的代码更改为任一

    const token = jwt.sign({linkedinUser}, config.secret, {expiresIn: 604800});
    //if you want to set expiration on the token
    

    const token = jwt.sign(linkedinUser.toJSON(), config.secret);
    //if you just want to sign the token without setting the expiration
    

    【讨论】:

      【解决方案2】:
      const token=jsonwebtoken.sign(user.toJSON(),config.secret,{expiresIn:30});
      

      用你的对象添加 .toJSON() 就可以了

      【讨论】:

        猜你喜欢
        • 2018-02-17
        • 2021-01-19
        • 2021-04-18
        • 1970-01-01
        • 2018-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多