我在路径徽章上收到验证错误,为什么我在徽章是数字的情况下得到验证错误,而我在保存的是数字?
检查我已经检查过:achievement
值为5achievement
类型是数字
错误:
(node:8260) UnhandledPromiseRejectionWarning: ValidationError: achievement_users validation failed: badgeid: Cast to Number failed for value "{ badgeid: 0,
progress: 0,
_id: 5c94c04a758c8a204440499e }" at path "badgeid"
保存代码:
var achievement = new achivementUsers();
achievement.badgeid = achievement;
return achievement.save().then(function (response) {
模式:
{
badgeid: {type: Number, default: 0},
progress: {type: Number, default: 0},
completed: {type: Boolean, default: false},
userid: {type: String, default: 'No name'},
}
最佳答案
您将覆盖变量achievement
。您正在使用新模型设置achievement
,因此您需要改用其他名称:
//somewhere in the code `achievement` is 5
var achievementUser = new achivementUsers();
achievementUser.badgeid = achievement;
return achievementUser.save().then(function (response) {});
或者,您可以在创建模型对象时初始化值。但是,对于不同的上下文使用不同的变量名称仍然是一个好习惯。
//somewhere in the code `achievement` is 5
var achievement = new achivementUsers({
badgeid: achievement
});
return achievement.save().then(function (response) {});