node.js mongojs findOne回调返回错误为null [英] node.js mongojs findOne callback returning error as null

查看:771
本文介绍了node.js mongojs findOne回调返回错误为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前被这个错误推向墙上。



我正在使用mongojs包装器为mongodb运行一个node.js应用程序。我在默认端口上启动mongod,然后运行

  var db = require('mongojs')connect('localhost:27017 / my_db'); 
var users = db.collection('users');
users.findOne({'fb_id':fbUserMetadata.id},function(err,user){
console.log(err);
console.log(user);
debugger;

});

但是err和user都是null。据我所知,err应该填充一些类型的数据,即使它没有找到任何东西。



如何让回调函数正常工作? c> findOne 查询不会执行。

找到至少一个匹配的文档,回调的第二个参数(在这种情况下 user )设置为 null 。这不是一个错误,因此 err 也是 null 。因此,您看到的是预期的不匹配查找响应。


Presently being driven up the wall by this error.

I am running a node.js app with the mongojs wrapper for mongodb. I Started mongod on the default port, then ran

var db = require('mongojs').connect('localhost:27017/my_db');
var users = db.collection('users');    
users.findOne({'fb_id' : fbUserMetadata.id}, function(err, user) {
                console.log(err);
                console.log(user);
                debugger;

    });

however err and user are both 'null'. To my knowledge, err should be populated with some kind of data even if it doesn't find anything.

How do I get the callback function to work properly? Pardon the newbie question.

解决方案

When the findOne query doesn't find at least one matching document, the second parameter of the callback (in this case user) is set to null. It's not an error, so err is also null. So what you're seeing is the expected no-match-found response.

这篇关于node.js mongojs findOne回调返回错误为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆