简体   繁体   English

如何通过JavaScript中的光标进行迭代

[英]how to iterate through cursor in javascript

I have three columns in mongoDB. 我在mongoDB中有三列。 I want to retrieve data from the DB using node.js. 我想使用node.js从数据库检索数据。 I used the below code. 我用下面的代码。

var myCursor = Users.find({USER_MOBILE_NUMBER : Auth.USER_MOBILE_NUMBER} , function(err , success){
        console.log('Response success '+success);
        console.log('Response error '+err);
    });

    myCursor.forEach(function(race) {
    console.log(race);    
    });

But when I run the code it returns null in the log for 'race'. 但是,当我运行代码时,它在日志中为“种族”返回null。 I am new to node.js and I also searched a lot to find a solution, but I failed. 我是node.js的新手,我也进行了大量搜索以查找解决方案,但失败了。 Please help me where I went wrong in the code. 请帮助我代码出错的地方。

It's an asynchronous function, you need to do your logic from within the callback as the find method doesn't actually return anything. 这是一个异步函数,您需要从回调中进行逻辑处理,因为find方法实际上不会返回任何内容。

Users.find({USER_MOBILE_NUMBER : Auth.USER_MOBILE_NUMBER} , function(err , myCursor){
    myCursor.forEach(function(race) {
        console.log(race);    
    });
});

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

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