简体   繁体   English

一次保存多个文档-Mongoose.js

[英]Saving multiple documents at once - Mongoose.js

user.save(function (err){
    if(err){
        throw err;
    }else{
        admin.save(function (err){
            if(err){
                throw err;
            }else{
                res.redirect('/home');
            }
       });
    }
});

This doesn't seem to work. 这似乎不起作用。 Any suggestions on how to save multiple documents at once? 关于如何一次保存多个文档的任何建议? Are there any other modules I could use except for mongoose to save them all? 除了猫鼬以外,还有其他我可以使用的模块来保存它们吗? Thank you. 谢谢。

I also tried this: 我也试过这个:

user.save(function (err){
     if(err)
        throw err;
     next();
});
admin.save(function (err){
     if(err)
        throw err;
     res.redirect('/home');
});

But that didn't work either! 但这也不起作用! Thanks again for your help 再次感谢你的帮助

app.post('/homeUser', function (req, res, next) {
            user.findOne({'accountOwner': req.user.userdata.username}, function (err, user){
                if(err)
                    throw err;
                if(user){
                    console.log('user found');
                    user.userdata.username = req.body.newUserName; // I have some other variables as well but they don't matter
                    user.save(function (err){
                        if(err)
                            throw err;
                        res.redirect('/home');
                    });
                } else if(!user) {
                    console.log('new user');
                    var newUser = new user();
                    //setting up some values for the newUser and some for the req.user
                    newUser.save(function (err){
                        if(err){
                            throw err;
                        }else{
                            req.user.save(function (err){ // the problem is here
                                if(err){
                                    throw err;
                                }else{
                                    res.redirect('/home');
                                }
                           });
                        }
                    });
                }
            });
    }

The error is at else{ req.user.save(function (err){ or at least at that line. (sorry if there are any spelling errors, I wrote this code just now, because I'm not using the computer for coding, but this is pretty much the code (except for some semicolons and curly brackets!)) 错误发生在其他地方{req.user.save(function(err){或至少在那一行。(很抱歉,如果有任何拼写错误,我刚才编写了这段代码,因为我没有使用计算机进行编码,但这几乎是代码(一些分号和大括号除外!))

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

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