Uncaught (in promise) Error: Request failed with status code 500

Uncaught (in promise) Error: Request failed with status code 500

我在用户 table 中有数据,但我无法根据他们的 ID 找到用户 - User.findById('kjsdfjsidjisj')。在 fetchuser 函数下,如果我 res.send({ user: user_id }) 它在解码 JWT 令牌后正确地以字符串格式发回 id。但是当我尝试 user = User.findById({ user_id }) 并发送给用户时,它给了我一个错误。也许我在查询中遗漏了什么??

我不知道如何完成这个承诺,如果是的话。


const User = require('../models/User');
const jwt = require('jwt-simple');
const config = require('../config/dev');

var mongoose = require('mongoose');

function tokenForUser(user) {
    const timestamp = new Date().getTime();
    return jwt.encode({ 
        sub: user.id,
        admin: user.admin
    }, config.secret);
}

exports.fetchuser = function (req, res, next) {
    const token = req.body.token;
    const secret = config.secret;
    const decoded = jwt.decode(token, secret);

    const user_id = decoded.sub;

    const user = User.findById({ user_id })

    res.send({
        user: user
    });
}

findById takes an id as param, not an object. And you need a callback to access the data.

User.findById(user_id, (err, user) => {
  res.send({
    user: user
  });
})