简体   繁体   中英

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!))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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