我有一个带有名称(必需)、描述(可选)的简单文档。在我的模型中,我使用有效的 id 更新了一个文档,并传递了值为 undefined 的描述,因为我想从文档中删除此属性。但是,我收到以下错误: message=Cast to string failed for value "undefined" at path "description", name=CastError, type=string, value=undefined, path=description。当用户不提供描述时,如何在更新时删除描述属性?可能吗?
谢谢
/*jslint indent: 2, node: true, nomen: true*/
'use strict';
var Schema = require('mongoose').Schema;
var mongoose = require('mongoose');
var mongooser = require('../../lib/mongooser');
// Schema
var schema = new Schema({
name: {
required: true,
set: mongooser.trimSetter,
trim: true,
type: String,
unique: true
},
description: {
set: mongooser.trimSetter,
trim: true,
type: String
}
});
// Export
module.exports = mongoose.model('Role', schema);
// Role.js
var update = function (model, callback) {
var test = { name: 'Users', description: undefined };
RoleSchema.findByIdAndUpdate(model.id, test, function (error, role) {
callback(error, role);
});
};