简体   繁体   English

未捕获(承诺)错误:请求失败,状态码为500

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

I have data in the User table, but I can't find a user based on their id - User.findById('kjsdfjsidjisj') . 我的用户表中有数据,但是找不到基于其ID的用户User.findById('kjsdfjsidjisj') Under the fetchuser function, if I res.send({ user: user_id }) it correctly sends back the id in a string format after decoding the JWT token. 在fetchuser函数下,如果我res.send({ user: user_id })它将在解码JWT令牌后以字符串格式正确发送回ID。 But when I try user = User.findById({ user_id }) , and sending user it gives me an error. 但是当我尝试user = User.findById({ user_id }) ,并发送用户给我一个错误。 Maybe I'm missing something in my query?? 也许我的查询中缺少什么?

I don't know how to complete this promise, if this is one. 如果这是一个承诺,我不知道如何完成这一承诺。


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. findByIdid作为参数,而不是对象。 And you need a callback to access the data. 并且您需要一个回调来访问数据。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Axios - 未捕获(承诺)错误:请求失败,状态码为 500 - Axios - Uncaught (in promise) Error: Request failed with status code 500 Axios createError.js:16 Uncaught (in promise) 错误:请求失败,状态码 500 - Axios createError.js:16 Uncaught (in promise) Error: Request failed with status code 500 未捕获(承诺)AxiosError {消息:'请求失败,状态码为 404'} - Uncaught (in promise) AxiosError {message: 'Request failed with status code 404'} 我收到 Uncaught (in promise) 错误:请求失败,状态码为 401 - I am getting Uncaught (in promise) Error: Request failed with status code 401 createError.js:16 Uncaught (in promise) 错误:请求失败,状态码为 400 - createError.js:16 Uncaught (in promise) Error: Request failed with status code 400 "message":"请求失败,状态码为 500","name":"Error","stack":"错误:请求失败,状态码为 500\\n - "message":"Request failed with status code 500","name":"Error","stack":"Error: Request failed with status code 500\n 当我将数据提交到数据库表时,我在浏览器控制台中遇到此错误:未捕获(承诺)错误:请求失败,状态代码为 404 - While I go to submit data into Database table I face this error in browser console: Uncaught (in promise) Error: Request failed with status code 404 未捕获(承诺中)错误:请求失败,状态代码 400 在 createError (createError.js:16) 在 XMLHttpRequest.onloadend 解决 - Uncaught (in promise) Error: Request failed with status code 400 at createError (createError.js:16) at settle at XMLHttpRequest.onloadend Axios - [未处理的承诺拒绝:错误:请求失败,状态码为 404] - Axios - [Unhandled promise rejection: Error: Request failed with status code 404] nodejs - AxiosError:请求失败,状态码为 500 - nodejs - AxiosError: Request failed with status code 500
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM