0

Please help me as I am new to node.js and mongodb. I need one help mongodb and node.js as I am not really confused to do that.

I have tried to get multiple user emailID in array from mongodb from the user's IDs (userID is unique) however I am getting only one value.

My code

 let output = {};

 console.log('')
 let Data = req.body;
 //array structure of users and payinvoiceID 
 let VZID = Data.username;
 let PARTICIPANTPAYMENTINFOS2 = Data.PARTICIPANTPAYMENTINFOS;
 console.log('PARTICIPANTPAYMENTINFOS2', PARTICIPANTPAYMENTINFOS2);

          console.log('Data', Data);
          console.log('Data', Data.PARTICIPANTPAYMENTINFOS[0].ID);
   var pARTICIPANTPAYMENTINFOs = [];
    for(let i = 0; i < Data.PARTICIPANTPAYMENTINFOS.length; i++){
        MongoClient.connect(config.Database.DFARM.connectString, function (err, client) {
            console.log(err)

            if (err) {
                let connError = new Error(500, "Error connecting to DFARM database", err);
                res.status(connError.status).json(connError);
            }
            else {
                console.log('Data', Data.PARTICIPANTPAYMENTINFOS[0].ID);

                let query = Data.PARTICIPANTPAYMENTINFOS[i].ID

                console.log('query', query);
                client.db(config.Database.DFARM.dbName).collection("User").find(query).toArray(function (err, docs) {
                    if (err) {
                        let connError = new Error(500, "Error while getting produce", err);
                        res.status(connError.status).json(connError);
                    }
                    else {
                            console.log('Docs', docs);
                        if(docs.length>0){
                                  console.log('EMAIL ID',docs[i].EMAIL);
                                 let EmailId = docs[i].EMAIL;
                                   console.log('EmailId', EmailId);
                            //    useremail.push(EmailId);
                            pARTICIPANTPAYMENTINFOs.push(EmailId);   
                            // pARTICIPANTPAYMENTINFOs.push(Data.PARTICIPANTPAYMENTINFOS[i].PAYABLEAMOUNT);         
                        }

                    }
                });

            }
            //client.close();
        });
    }
    console.log('PARTICIPANTPAYMENTINFOS', PARTICIPANTPAYMENTINFOS);

I am getting only one email ID i.e EMAIL ID pc@gmail.com.

any help will be very useful me.

Thanks in advances.

Abhirock
  • 425
  • 6
  • 17
  • Also please note [How to properly reuse connection to Mongodb across NodeJs application and modules](https://stackoverflow.com/questions/24621940/how-to-properly-reuse-connection-to-mongodb-across-nodejs-application-and-module). In addition to the general aspects of learning how to work with asynchronous operations in general, the other thing you **really** should learn is that database connections are made **once**, and most **certainly not** *within* a loop. – Neil Lunn Oct 24 '19 at 10:50
  • thanks@NeilLunn for this comment it resolve my other issue which I am facing – Abhirock Oct 24 '19 at 10:52

0 Answers0