当我尝试执行update
函数时,出现此错误。当我添加timestamps: true
我导航到错误,然后在mongoose >> lib文件夹的schema.js中调用了此函数。
function _setTimestampsOnUpdate(next) {
applyTimestampsToUpdate(createdAt, updatedAt, this.getUpdate(),
this.options, true);
applyTimestampsToChildren(this);
next();
}
以上功能与getUpdate
中的schema.js
一起使用。请帮我解决这个问题。
::方案::
const assetListSchema = new Schema({userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
name: String,
config: {
showAssetsLibrary: {
type: Boolean,
required: true,
default: true
},
showAssetsDetails: {
type: Boolean,
required: true,
default: true
},
aliasName: {
type: String,
maxlength: 75,
default: ""
}
}},{ timestamps: true});
:: UPDATE函数::
assetListSchema.methods.update = async function (data) {
try {
let asst = this;
data = _.pick(data, ['showAssetsLibrary',
'showAssetsDetails','aliasName']);
if (_.isEmpty(data)) {
emptyError();
}
return await AssetsHierarchy.findByIdAndUpdate(asst._id, {
config: data
}, {
new: true,
fields: {
"config": 1
}
});
} catch (err) {
throw new Error(err.message);
}
}
::错误::
TypeError:this.getUpdate不是函数
在model._setTimestampsOnUpdate(D:\ inspectionApp \ inspection-backend \ node_modules \ mongoose \ lib \ schema.js:873:56)
在callMiddlewareFunction(D:\ inspectionApp \ inspection-backend \ node_modules \ kareem \ index.js:427:23)
接下来(D:\ inspectionApp \ inspection-backend \ node_modules \ kareem \ index.js:58:7)
在Kareem.execPre(D:\ inspectionApp \ inspection-backend \ node_modules \ kareem \ index.js:86:8)
在Kareem.wrap(D:\ inspectionApp \ inspection-backend \ node_modules \ kareem \ index.js:265:8)
在模型上$ __ update(D:\ inspectionApp \ inspection-backend \ node_modules \ kareem \ index.js:339:11)
在utils.promiseOrCallback.callback(D:\ inspectionApp \ inspection-backend \ node_modules \ mongoose \ lib \ helpers \ model \ applyHooks.js:80:30)
承诺(D:\ inspectionApp \ inspection-backend \ node_modules \ mongoose \ lib \ utils.js:246:5)
在新的Promise()中
在Object.promiseOrCallback(D:\ inspectionApp \ inspection-backend \ node_modules \ mongoose \ lib \ utils.js:245:10)
at model.objToDecorate。(匿名函数)[作为更新](D:\ inspectionApp \ inspection-backend \ node_modules \ mongoose \ lib \ helpers \ model \ applyHooks.js:79:20)
更新时(D:\ inspectionApp \ inspection-backend \ App \ controllers \ settings \ asset-hierarchy \ assetsHierarchyController.js:163:33)
在Layer.handle [作为handle_request](D:\ inspectionApp \ inspection-backend \ node_modules \ express \ lib \ router \ layer.js:95:5)
下一个(D:\ inspectionApp \ inspection-backend \ node_modules \ express \ lib \ router \ route.js:137:13)
在Route.dispatch(D:\ inspectionApp \ inspection-backend \ node_modules \ express \ lib \ router \ route.js:112:3)
在Layer.handle [作为handle_request](D:\ inspectionApp \ inspection-backend \ node_modules \ express \ lib \ router \ layer.js:95:5)