循环首先执行,但它的任务响应在节点js

时间:2017-09-06 06:58:52

标签: node.js express

嗨,我想要做的是我有联系人数组,我想迭代这个并找到数据库中的联系电子邮件。目前这些电子邮件在数据库中不存在,因此每次我们从结果中获取null。所以在结果null我想在'conlist Array'中添加这个用户详细信息并在'conlist2 Array'中添加用户电子邮件,在此循环之后,我的代码的其余部分依赖于这些Array'conlist和conslist2'

这是我的代码:

let conlist2=[];
let conlist=[];

var Contacts=[
    {"FirstName":"Ramesh","LastName":"sharma","Email":"gh6@xyz.com","Phone":"(465) 413-4564"},{"FirstName":"Rajesh","LastName":"sharma","Email":"ankit@xyz.com","Phone":"(465) 413-4564"}
];

          Contacts.forEach(function(element,cb) {

                console.log("loop starts");
                if (element.Email == null)
                    {
                        return false;
                    }
                    existContact=null;
                    contactModel=null;                                                          

                                    let collection = db.collection("users");                                             
                                    collection.findOne({email:element.Email},function(err,data){

                                if(err){                                                                          
                                    res.json(err);
                                }                                                                     
                                existContact=data;                                                                                                                                    
                                if(existContact==null){
                                    console.log("contact not found");
                                    conlist.push(element);  
                                    console.log(conlist);                                                                       
                                }                                                                       
                                conlist2.push(element.Email);
                                console.log(conlist2);   

                                    cb();
                            });           
                            console.log("loop ends");      
            },function(err){
                    if(err){throw err;}

                    console.log("completed");

                    }); 

                    if(conlist2.length>0)
                    {
                        console.log("hurrey succeed");
                    }
                    else
                        {
                            console.log("u came early");
                        }

这是我得到的输出:

loop starts <br>
loop ends <br>
loop starts <br>
loop ends <br>
u came early <br>
contact not found <br>
[ { FirstName: 'Ramesh',
    LastName: 'sharma',
    Email: 'gh6@xyz.com',
    Phone: '(465) 413-4564' } ]
[ 'gh6@xyz.com' ] <br>
contact not found <br>
[ { FirstName: 'Ramesh',
    LastName: 'sharma',
    Email: 'gh6@xyz.com',
    Phone: '(465) 413-4564' },
  { FirstName: 'Rajesh',
    LastName: 'sharma',
    Email: 'ankit@xyz.com',
    Phone: '(465) 413-4564' } ]
[ 'gh6@xyz.com', 'ankit@xyz.com' ] <br>
completed <br>

/ * --------------------------------------------- ----- * /

但我想要输出如下:

loop starts <br>
contact not found <br>
[ { FirstName: 'Ramesh',
    LastName: 'sharma',
    Email: 'gh6@xyz.com',
    Phone: '(465) 413-4564' } ]
[ 'gh6@xyz.com' ] <br>
loop ends <br>
loop starts <br>
contact not found <br>
[ { FirstName: 'Ramesh',
    LastName: 'sharma',
    Email: 'gh6@xyz.com',
    Phone: '(465) 413-4564' },
  { FirstName: 'Rajesh',
    LastName: 'sharma',
    Email: 'ankit@xyz.com',
    Phone: '(465) 413-4564' } ]
[ 'gh6@xyz.com', 'ankit@xyz.com' ] <br>
loop ends <br>
completed <br>
hurrey succeed <br>

我们将不胜感激 谢谢。

2 个答案:

答案 0 :(得分:0)

nodejs本质上是异步的,当您点击数据库调用nw请求时,进程不等待响应并执行更多行,因此使用async / await或使用异步库async_lib

使函数同步

for async await方法查看Async/await

答案 1 :(得分:0)

使用async / await或async包。 async / await是更可取的。