【发布时间】:2016-01-29 19:51:10
【问题描述】:
我有这个代码:
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);
}
);
});
当我这样邮递员时:
我收到以下错误:
{ "name": "MongoError", "message": "异常:字段 'challengesuggestions' 必须是一个数组,但属于 OID 类型 文档 {_id: ObjectId('56263b910d1a2f1f0077ffae')}", "errmsg": “例外:字段 'challengesuggestions' 必须是一个数组,但 文档中的 OID 类型 {_id: ObjectId('56263b910d1a2f1f0077ffae')}", “代码”:16837,“确定”: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”,难度:“中等”,名称: “Probeer 1 van onze recepten.”,__v:0,childfriendly: true…}
这给了我以下错误:
D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\mongoose\lib\schema\obj ectid.js:134 throw new CastError('ObjectId', value, this.path); ^ 错误 在 MongooseError.CastError (D:\Stijn\Documenten\EVA-project-Groep-6\Api\node _modules\mongoose\lib\error\cast.js:18:16) 在 ObjectId.cast (D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\m ongoose\lib\schema\objectid.js:134:13) 在 Array.MongooseArray.mixin._cast (D:\Stijn\Documenten\EVA-project-Groep-6\ Api\node_modules\mongoose\lib\types\array.js:124:32) 在 Array.MongooseArray.mixin._mapCast (D:\Stijn\Documenten\EVA-project-Groep -6\Api\node_modules\mongoose\lib\types\array.js:295:17) 在 Object.map (本机) 在 Array.MongooseArray.mixin.push (D:\Stijn\Documenten\EVA-project-Groep-6\A pi\node_modules\mongoose\lib\types\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\kareem\index.js:177:19 在 D:\Stijn\Documenten\EVA-project-Groep-6\Api\node_modules\mongoose\node_mo dules\kareem\index.js:109:16 在 doNTCallback0 (node.js:408:9) 在 process._tickCallback (node.js:337:13) 10 月 29 日 22:05:38 - [nodemon] 应用程序崩溃 - 在启动之前等待文件更改...
我该如何解决这个问题?
【问题讨论】:
-
你能告诉我们架构定义吗?
标签: arrays mongodb mongoose postman