繁体   English   中英

如何从Node.js中的find返回,mongoose mongodb集合

[英]How to return from find in nodejs,mongoose mongodb collection

当使用mongoose在mongodb集合中使用find()元素时,不会在Test.js中执行console.log

Test.js中console.log不会打印MongoDb集合中的数据

var model =  require('./model');
model.findbyTag('java',function (data)
{
    console.log(data)
});

Model.js具有以下条目

exports.findbyTag = function(tag,out)
{
    var condtion = {"tag" : tag}
     Tag.find(condtion,function(err,out){
          //  console.log(out);
             if (err) console.log('Error returning Tag!');
            else {
               return out;
            }
        });
}

当我取消注释Model.js中的console.log文件时,它会记录find查询中匹配的数据,

test.js文件中的回调未执行,我是否已成功从Model.js返回数据,我在做什么错?

在Model.js中,findbyTag方法将返回函数对象,而不是执行回调函数。 另外,需要将查询的文档作为参数传递给回调函数。 可以像这样修改:

exports.findbyTag = function(tag,out)
{
    var condtion = {"tag" : tag}
     Tag.find(condtion,function(err,doc){
          //  console.log(doc);
             if (err) console.log('Error returning Tag!');
            else {
               out(doc);
            }
        });
}

暂无
暂无

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

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