如何在mongoose

时间:2015-10-29 15:20:12

标签: arrays mongodb mongoose postman

我有这段代码:

router.post('/setsuggestions', function(req, res, next){
  if(!req.body.username || !req.body.challengessuggestions){
    return res.status(400).json({message: challengessuggestions});
  }

  var query = { username: req.body.username };
  /*User.findOneAndUpdate(query, { challengessuggestions: req.body.challengessuggestions }, callback = function(response){
    res.json(response);
  });*/
/*
  User.findOneAndUpdate(
    query,
    {$push: {"challengessuggestions": {$oid: req.body.challengessuggestions}}},
    callback = function(response) {
        res.json(response);
    }
    );*/

  User.findOneAndUpdate(
    query,
    {$push: {challengessuggestions: req.body.challengessuggestions}},
    {safe: true, upsert: true},
    function(err, model) {
         res.json(err);
    }
);


});

当我像这样邮递员时:

Postman config

我收到以下错误:

  

{" name":" MongoError"," message":" exception:The field   ' challengessuggestions'必须是一个数组,但是类型为OID   文件{_id:ObjectId(' 56263b910d1a2f1f0077ffae')}"," errmsg":   "例外:该领域的挑战建议'必须是一个数组,但是   文档{_id:ObjectId(' 56263b910d1a2f1f0077ffae')}"中的OID类型,   "代码":16837," ok":0}

这是AppUser的架构定义:

var UserSchema = new mongoose.Schema({
    username: { type: String, lowercase: true, unique: true },
    firstname: { type: String},
    lastname: { type: String},
    difficulty: { type: String},
    isstudent: { type: Boolean },
    haschildren: { type: Boolean},
    gender: { type: String },
    email: { type: String, unique: true},
    birthdate: String,
    isdoingchallenges: { type: Boolean },
    challengescompleted: [{ type: ObjectId, ref: 'Challenge' }],
    currentchallenge: { type: ObjectId, ref: 'Challenge' },
    challengessuggestions: [{ type: ObjectId, ref: 'Challenge' }],
    hash: String,
    salt: String
});

这是挑战的架构定义:

var Challengeschema = new mongoose.Schema({
    name: { type: String,  initial: true, required: true, index: true },
    image: { type: Array },
    difficulty: { type: String },
    studentfriendly: { type: Boolean },
    childfriendly: { type: Boolean },
    description: { type: String }
});

我在调用api的函数中发送它:

  

对象{_id:" 5631423f8c5ba50300f2b4f6",难度:"中",名称:   "探测器1 van onze recepten。",__ v:0,childfriendly:true ...}

这给了我以下错误:

  

d:\斯泰恩\ Documenten \ EVA-项目Groep的-6 \阿比\ node_modules \猫鼬\ lib中\架构\ OBJ   ectid.js:134         抛出新的CastError(' ObjectId',value,this.path);         ^错误       在MongooseError.CastError(D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ Api \ node)   _modules \猫鼬\ lib中\错误\ cast.js:18:16)       在ObjectId.cast(D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ Api \ node_modules \ m   ongoose \ LIB \架构\ objectid.js:134:13)       在Array.MongooseArray.mixin._cast(D:\ Stijn \ Documenten \ EVA-project-Groep-6 \   API \ node_modules \猫鼬\ lib中\类型\ array.js:124:32)       在Array.MongooseArray.mixin._mapCast(D:\ Stijn \ Documenten \ EVA-project-Groep)   -6 \阿比\ node_modules \猫鼬\ lib中\类型\ array.js:295:17)       at Object.map(native)       在Array.MongooseArray.mixin.push(D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ A   PI \ node_modules \猫鼬\ lib中\类型\ array.js:308:25)       在查询。 (D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ Api \ routes \ ind ex.js:144:44)       在D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ Api \ node_modules \ mongoose \ node_mo   dules \卡里姆\ index.js:177:19       在D:\ Stijn \ Documenten \ EVA-project-Groep-6 \ Api \ node_modules \ mongoose \ node_mo   dules \卡里姆\ index.js:109:16       在doNTCallback0(node.js:408:9)       at process._tickCallback(node.js:337:13)10月22日22:05:38 - [nodemon]应用程序崩溃 - 在启动之前等待文件更改...

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

首先使用 findOne() 查询用户用户,并使用第一个找到的回传文件来保存嵌入文档:

router.post('/setsuggestions', function(req, res, next){
    if(!req.body.username || !req.body.challengessuggestions){
        return res.status(400).json({message: challengessuggestions});
    }

    var query = { username: req.body.username };

    User.findOne(query, function (err, user){       
        if (err) //throw ...
        if (user) {
            if (user.challengessuggestions && user.challengessuggestions.length) {
                user.challengessuggestions.push(req.body.challengessuggestions);
            }
            else {
                user.challengessuggestions = [req.body.challengessuggestions];
            }

            // save changes
            user.save(function (err) {
                if (!err) {
                    // done ...
                }
            });
        }
    });    
);