Javascript 无法使用FindUpdate mongoose更新架构
我在模式中有一个代码: 模型 更新Javascript 无法使用FindUpdate mongoose更新架构,javascript,node.js,database,mongodb,mongoose,Javascript,Node.js,Database,Mongodb,Mongoose,我在模式中有一个代码: 模型 更新 TypeSchema.pre('findOneAndUpdate', function(next){ console.log(this.title) // undefined this.alt = slug(this.title); next(); }); 在这里,我在这个标题中没有定义 和api中的请求: router.put('/arrivals/products/types/:id', isAuthenticated, async (req
TypeSchema.pre('findOneAndUpdate', function(next){
console.log(this.title) // undefined
this.alt = slug(this.title);
next();
});
在这里,我在这个标题中没有定义
和api中的请求:
router.put('/arrivals/products/types/:id', isAuthenticated, async (request, response, next) => {
jsonPreProcessor.response = response;
let id = request.params.id;
let title = request.body.title;
console.log('router ', title); // title is working
if (id === undefined) {
return jsonPreProcessor.error("Id не указан");
}
if (title === undefined) {
return jsonPreProcessor.error("Title не указан");
}
Type.findByIdAndUpdate(id, {
title: title
}, {
new: true
}).then(type => {
return jsonPreProcessor.success(type);
}).catch(error => {
return jsonPreProcessor.error(error.message); // here I got an error message that title is undefined (in pre.('findOneAndUpdate'))
});
});
但在这里我得到了一个头衔
也许我使用了“findOneAndUpdate”之前的不正确?如果是,有人能帮我吗我已经解决了我的问题:
而不是这个
TypeSchema.pre('findOneAndUpdate', function(next){
console.log(this.title) // undefined
this.alt = slug(this.title);
next();
});
我写
TypeSchema.pre('findOneAndUpdate', function(next){
this.update({
$set: {
alt: slug(this.getUpdate().title)
}
});
next();
});
结果是完美的
TypeSchema.pre('findOneAndUpdate', function(next){
this.update({
$set: {
alt: slug(this.getUpdate().title)
}
});
next();
});