在api调用中更新2个mongoose模式

sin*_*Gob 9 mongoose mongodb node.js mean-stack

目前我正在尝试在api调用中更新两个不同的用户架构.

第一个模式记录在用户模式中,我们给它命名= Tom第二个模式是注册该应用程序的其他用户,我们给它命名= John

架构代码

schema.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');



var UserSchema = new Schema({
    name: String,
    username: { type: String, required: true, index: { unique: true }},
    password: { type: String, required: true, select: false },
    followers: [{ type: Schema.Types.ObjectId, ref: 'User'}],
    following: [{ type: Schema.Types.ObjectId, ref: 'User'}],
    followersCount: Number,
    followingCount: Number

});


module.exports = mongoose.model('User', UserSchema);
Run Code Online (Sandbox Code Playgroud)

api名称是'/ follow /:user_id',我想要实现的是.每当用户Tom跟随John这样的其他用户时,Tom的跟随字段将被更新以及John的跟随者字段.

我当前的尝试(req.decoded.id是登录用户)

api.js

// The first way

apiRouter.post('/follow/:user_id', function(req, res) {
    User.findOneAndUpdate(
    {   

        _id: req.decoded.id, 
        following: { $ne: req.params.user_id }
    }, 

    { 
        $push: { following: req.params.user_id},
        $inc: { followingCount: 1}

    },
    function(err, currentUser) {
        if (err) {
            res.send(err);
            return;
        }
        console.log(currentUser);

    });
    User.findOneAndUpdate(
    {

        _id: req.params.user_id,
        followers: { $ne: req.decoded.id } 

    },

    {
        $push: { followers: req.decoded.id },
        $inc: { followersCount: 1}

    }, function(err, user) {
        if(err) {
            res.send(err);
            return;
        }
        res.json({
            message: "Successfully followed"
        });
    }
    )
});


//Second way

apiRouter.post('/follow/:user_id', function(req, res) {

    // find a current user that has logged in
        User.update(
            {   
                _id: req.decoded.id, 
                following: { $ne: req.params.user_id } 
            }, 

            { 
                $push: { following: req.params.user_id},
                $inc: { followingCount: 1}

            },
            function(err) {
                if (err) {
                    res.send(err);
                    return;
                }

                User.update(
                    {
                        _id: req.params.user_id,
                        followers: { $ne: req.decoded.id }
                    },

                    {   
                        $push: { followers: req.decoded.id },
                        $inc: { followersCount: 1}

                    }

                ), function(err) {
                    if(err) return res.send(err);

                    res.json({ message: "Successfully Followed!" });
                }

        });
});
Run Code Online (Sandbox Code Playgroud)

两者都有问题,

第一种方式:问题是'无法设置已发送的标头',因为在一次api调用中有两个单独的mongoose查询,它响应两次,这就是我得到该错误的原因.

第二种方式:问题是,登录用户(Tom)的以下字段得到更新,而其他用户的关注者字段(John)返回null.我控制记录两个值,并使用POSTMAN chrome app测试它.

借给我你的想法!

小智 0

第一种方式可以通过两种方式进行改进。一种是在更新关注字段的回调中更新关注者字段。另一种方法是使用异步瀑布。我建议使用 async-waterfall(npm async-waterfall)。