使用mongoose在mongodb中的findOne结果返回null

时间:2018-06-28 09:55:36

标签: node.js mongodb express mongoose

当我运行此代码时-输出为空

Card.findOne({userId : model.userId} , function (err, cardData) {
        console.log(err); //output null
        console.log(cardData); //output null
    });

如果存在card和cardData,则预期输出null

  

console.log(err); //输出null

     

console.log(cardData); //输出cardData {json格式}

示例文档

{
    "_id" : ObjectId("5b32712d90bd3e000454ce92"),
    "reactions" : {
        "likes" : [],
        "disLikes" : [],
        "comments" : []
    },
    "code" : "1qf28",
    "creationDate" : ISODate("2018-06-26T17:00:29.542Z"),
    "reports" : [],
    "userId" : "5b2bee8c47ca27344f5425cf",
    "name" : "Ahmed Mohsen",
    "phone" : "01233556",
    "gender" : "male",
    "public" : true,
    "position" : "Developer ",
    "subtitle" : "Free costa",
    "img" : "https://i.imgur.com/2qLGIWa.jpg",
    "__v" : 0
}

更新

userId未定义

但是我通过此代码发送

axios.get(settings.default.baseUrl + '/api/cards/check_user_card' , {userId : userId})

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

var ObjectId = require('mongoose').Types.ObjectId;
Card.findOne({userId : new ObjectId(model.userId)} , function (err, cardData) {
    console.log(err); //output null
    console.log(cardData); //output null
});