简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z findById function 返回奇怪的响应

[英]Mongoose findById function returning weird response

I am creating an Rest API using node.js,express and mongoDb.我正在创建一个 Rest API 使用 node.js,express 和 ZFC7EAA913F6113150935505830ZCF7。 I had an endpoint for user registration which simply adds user details into the database.我有一个用于用户注册的端点,它只是将用户详细信息添加到数据库中。

User Schema looks like this:-用户架构如下所示:-

const userSchema= new mongoose.Schema({
    name:{
        type:String,
        required:true
    },
    referenceId:{
        type:String,
        default: ''
    },
    password:{
        type:String,
        required:true
    }
})

User registration part is working fine as expected.用户注册部分按预期工作正常。

I also need an endpoint which takes user id and returns the user details in json format.我还需要一个端点,它接受用户 ID 并以 json 格式返回用户详细信息。 This is what i have coded so far.这是我到目前为止编码的内容。

/*
    This endpoint returns the data of a single user with given user id
*/
router.get('/:id',async(req,res)=>{
    //This function will return the registered user with given id.
    try{
        const user=await User.findById(req.params.id);
        if(!user){
            res.send("Could not found");
        }
        res.json(user);
    }catch(err){
        res.send(err);
    }
})

This part is also working fine if the user exists with the given id.如果用户以给定的 id 存在,这部分也可以正常工作。 If user does not exists i need to return an custom error message.如果用户不存在,我需要返回自定义错误消息。 But in that case below response is returned.但在这种情况下,将返回以下响应。

{
    "stringValue": "\"5eecac810ea3711230b58a9\"",
    "kind": "ObjectId",
    "value": "5eecac810ea3711230b58a9",
    "path": "_id",
    "reason": {}
}  

So my question is what is this weird response and how could i handle user not found case.所以我的问题是这个奇怪的反应是什么,我该如何处理用户未找到的情况。

BTW 5eecac810ea3711230b58a9 is the userid i searched for which does not exists. BTW 5eecac810ea3711230b58a9是我搜索的不存在的用户标识。

EDIT:-编辑:-

After a little bit of debugging i found that the program control transferred to the catch block and the response is generated in the catch block instead of try block.经过一点调试,我发现程序控制转移到了 catch 块,并且响应是在 catch 块而不是 try 块中生成的。

/*
    This endpoint returns the data of a single user with given user id
*/
router.get('/:id',async(req,res)=>{
    //This function will return the registered user with given id.
    console.log('Here');
    try{
        const user=await User.findById(req.params.id);
        console.log(user._id);
        res.json(user);
    }catch(err){
        //This works when user is not found
        res.send("Could Not found");
    }
})

Not finding any records isn't an error condition.没有找到任何记录不是错误情况。 To check if the user doesn't exist you could check if a specific field exists rather than if(!user) .要检查用户是否不存在,您可以检查特定字段是否存在而不是if(!user)

Something like就像是

if(!user.id){
   res.send("Could not found");
}

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

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