node.js - 使用 mongoose 在 mongodb 中 findOne 结果返回集合的空值
问题描述
当我运行此代码时 - 它输出 null
Card.findOne({userId : model.userId} , function (err, cardData) {
console.log(err); //output null
console.log(cardData); //output null
});
如果卡存在且 cardData 则预期输出 null
控制台日志(错误);//输出空
控制台.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})
解决方案
试试这个代码:
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
});
推荐阅读
- regex - Google Analytics 中的正则表达式双斜杠
- c# - C# - 创建没有空属性的对象
- javascript - 根据 2 键从数组中删除重复对象
- php - 我是zend 3 的新手。当我安装 zend 并点击 url (localhost/zf3/public) 然后我得到了这个错误
- laravel - 在 Laravel5.7 中验证数组上的重复值
- javascript - 在框架 vue 上绑定
- python - 使用三个点 (...) 打破冗长的行会给出无效的语法
- php - 在 Drupal 8 安装 mongodb 模块时出现语法错误
- matlab - Matlab 到 volttron 集成
- python - 在 Django 中下载多个文件而不创建 zip