我用findOne调用一个简单的查询,到目前为止,users.db中的数据是:
{"id":40, "is_admin":1,"name":"John Smith"},
{"id":43, "is_admin":1,"name":"Laura Smith"}
// Users
var users = new Datastore({ filename: 'db/users.db' });
var id_user = 43;
console.log("-------------geting users db");
//
users.loadDatabase(function (err) {
console.log("------------- users db loaded--", id_user);
// find user by id
users.findOne({ id: id_user }, function (err, a,b,c) {
console.log(a,b,c); // displays null undefined undefined
});
});你知道为什么返回null吗?
发布于 2017-05-04 05:37:53
我认为在findOne中传递的函数应该有两个参数。第一个参数是查询的结果,第二个参数是错误的。第一个参数将为空,如果在db .otherwise中没有匹配,则应返回匹配结果。
函数(结果,错误){
}它将是您的函数原型
发布于 2017-05-04 17:48:56
我在我的应用程序中测试了你的代码
db.findOne({ "c2cId":"292" }, function (err, a,b,c) {
console.log(a,b,c);
});它返回一个文档,并且未定义、未定义。如果您使用findOne,我猜您只是想找到第一个文档,所以查看doc at,您可以看到只有两个参数err和关于null的doc:在您的查询中,您是否使用了一个名为id的变量?或者这是关键,在最后一种情况下,你应该使用引号...
https://stackoverflow.com/questions/43619428
复制相似问题