如果不将Id推入数组,如何检查mongodb数组中是否存在Id

时间:2017-04-08 16:56:43

标签: node.js mongodb mongoose-schema

我正在使用mongoose进行架构设计我想检查用户集合,如果设备ID存在于am数组中,如果false将设备推入用户设备数组

这是ma用户架构

var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

var deviceSchema = mongoose.Schema(
{
    macAddress: {type: String, required: true, unique: true},
    createdAt: {type: Date, default: Date.now},
}
)
var Device = mongoose.model('Device', deviceSchema);

module.exports = Device;

这是我的用户架构

var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var userSchema = Schema(
{
    firstname: {type: String, required: true},
    lastname: {type: String, required: true},
    email: {type: String, required: true, unique: true},
    username: {type: String, required: true, unique: true},
    password: {type: String, required: true},
    createdAt: {type: Date, default: Date.now},
    devices: [{ type: Schema.Types.ObjectId, ref: 'Device1' }]
}
)

var User = mongoose.model('User', userSchema);

module.exports = User;

这是我的nodejs路由器

router.post('/user/device/:username', function(req, res, next) {
 if(!req.body.macAddress) {
    sendJSONresponse(res, 400, {
        "message": "Device Mac-Address required"
    });
    return;
}
 User.findOne({ username: req.params.username }, function(err, user) {
  if (err) { return next(err); }
  if (!user) { return sendJSONresponse(res, 400, {
        "message": "user not found"
    });
  }
        Device.findOne({macAddress : req.body.macAddress},    function(err, device){
        if(err){return next(err);}
        if(!device){
           sendJSONresponse(res, 400, {
                "message": "No device with that macaddress"
            });
           return;
        }
        User.find({ devices: { $elemMatch: { $eq: req.body.macAddress} } }, function(err, users){
                if(err){return next(err);}
                if(users){
                    sendJSONresponse(res, 400, {
                        "message": "Device already assigned to a user"
                    });
                    return;
                }else{
                    user.devices.push(device._id);
                      user.save(function(err) {
                        if(err){return next(err);}
                        sendJSONresponse(res, 200, user);
                      });
                }
            });

    });
  });
});

完成所有这些后,当我尝试使用带邮递员的api时告诉我服务器无法发送回复:请帮我修复我的代码

2 个答案:

答案 0 :(得分:0)

使用调试器逐步完成代码,看看发生了什么。如果您不知道如何调试,请查看structured streaming approach。这是一个免费的工具。

答案 1 :(得分:0)

您正在将设备ID推送到设备阵列

user.devices.push(device._id)

但你正在尝试elemMatch macAddress

User.find({ devices: { $elemMatch: { $eq: req.body.macAddress} } }..

什么时候应该

User.find({ devices: { $elemMatch: { $eq: device._id} } }..

您必须将elemMatch param更改为device._id以使其正确。

router.post('/user/device/:username', function(req, res, next) {
    if(!req.body.macAddress) {
        sendJSONresponse(res, 400, {
            "message": "Device Mac-Address required"
        });
        return;
    }
    User.findOne({ username: req.params.username }, function(err, user) {
        if (err) { return next(err); }
        if (!user) { return sendJSONresponse(res, 400, {
            "message": "user not found"
        });
        }
        Device.findOne({macAddress : req.body.macAddress},    function(err, device){
            if(err){return next(err);}
            if(!device){
                sendJSONresponse(res, 400, {
                    "message": "No device with that macaddress"
                });
                return;
            }
            User.find({ devices: { $elemMatch: { $eq: device._id} } }, function(err, users){
                if(err){return next(err);}
                if(users){
                    sendJSONresponse(res, 400, {
                        "message": "Device already assigned to a user"
                    });
                    return;
                }else{
                    user.devices.push(device._id);
                    user.save(function(err) {
                        if(err){return next(err);}
                        sendJSONresponse(res, 200, user);
                    });
                }
            });

        });
    });
});