簡體   English   中英

如何使用Mongodb和Node JS在多個Collection中保存數據

[英]How to Save data in multiple Collection using Mongodb and node js

我在我的項目之一中使用nodeJS和mongodb。

我試圖在一個保存按鈕中將數據保存在多個集合中。

我用來實現此目的的代碼如下:

var lastInsertId;
loginData={
        userName: req.body.txtUsername,
        password: req.body.txtPassword,
        active:1,
        createdOn:new Date(),
        updatedOn:new Date()
    };
var dbo = db.db("test");
dbo.collection("login").insertOne(loginData, function(err, result) {
        if (err) throw err;
        lastInsertId=result.insertedId;
}); 

if(lastInsertId){
            usersData={
                email: req.body.txtEmail,
                firstName: req.body.txtFirstName,
                lastName:req.body.txtLastName,
                mobileNumber:req.body.txtMobileNumber,
                login_id:lastInsertId,
                active:1,
                createdOn:new Date(),
                updatedOn:new Date()
            };

            dbo.collection("users").insertOne(usersData, function(err, result) {
                if (err) throw err;
                console.log('saved to users');        
                });
        }       

你能告訴我上面代碼有什么問題嗎?

謝謝。

問候,薩羅尼

我想對上述問題進行解釋。

var lastInsertId;  //it is undefined right now



//The following function is an asynchronous function. 
var dbo = db.db("test");
dbo.collection("login").insertOne(loginData, function(err, result) {
        if (err) throw err;
        lastInsertId=result.insertedId;
}); 

/*NodeJs doesn't run synchronously. So, while it is running
above function i.e. insertOne, without it being completed it 
reaches here.
Since, the above function is not completed
executing, lastInsertId will be undefined still. 
So, the following block of code doesn't run */



     if(lastInsertId){  //this block won't run
                usersData={
                    //key-value pairs
                };

                dbo.collection("users").insertOne(usersData, function(err, result) {
                    if (err) throw err;
                    console.log('saved to users');        
                    });
      }      


    /*The solution to the above problem can be achieved by putting the 
    code block inside 'if(lastInsertId)' in callback of insert login. 
    So it would run only after the execution of insertOne function.*/ 

    //Therefore the correct code would be: 
    var dbo = db.db("test");
    dbo.collection("login").insertOne(loginData, function(err, result) {
            if (err) throw err;
            lastInsertId=result.insertedId;


           if(lastInsertId){  //this block will run
            usersData={
                //key-value pairs
            };

            dbo.collection("users").insertOne(usersData, function(err, result) {
                if (err) throw err;
                console.log('saved to users');        
                });
  }  
    }); 

我認為像這樣的插入登錄功能的回調內部移動IF塊應該工作

var lastInsertId;
loginData = {
    userName: req.body.txtUsername,
    password: req.body.txtPassword,
    active: 1,
    createdOn: new Date(),
    updatedOn: new Date()
};
var dbo = db.db("test");
dbo.collection("login").insertOne(loginData, function (err, result) {
    if (err) throw err;
    lastInsertId = result.insertedId;

    if (lastInsertId) {
        usersData = {
            email: req.body.txtEmail,
            firstName: req.body.txtFirstName,
            lastName: req.body.txtLastName,
            mobileNumber: req.body.txtMobileNumber,
            login_id: lastInsertId,
            active: 1,
            createdOn: new Date(),
            updatedOn: new Date()
        };

        dbo.collection("users").insertOne(usersData, function (err, result) {
            if (err) throw err;
            console.log('saved to users');
        });
    }

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM