4

名前 (必須)、説明 (オプション) を含む単純なドキュメントがあります。私のモデルでは、有効な ID でドキュメントを更新し、ドキュメントからこのプロパティを削除したいので、値が未定義の説明を渡します。ただし、次のエラーが発生しました: 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);
    });
};
4

2 に答える 2