Node.js 从Mongoose中间件访问旧模型属性
我有一个Node.js 从Mongoose中间件访问旧模型属性,node.js,mongoose,Node.js,Mongoose,我有一个类别模型,它的唯一属性是名称 还有一个子类别模型,它有一个名称和一个类别字符串字段,其中包含它的类别模型的名称 当类别模型的名称更改时,指向该类别的每个子类别应更新其类别字段以匹配新名称 我目前直接在路由处理程序上实现这一点,方法是获取指向某个类别的所有子类别,通过它们进行迭代并更新其类别字段 我想在模型中实现这个逻辑,它应该属于哪里 我在Category模型上创建了一个预中间件 CategorySchema.pre('save', function(next) { console.
类别
模型,它的唯一属性是名称
还有一个子类别
模型,它有一个名称
和一个类别
字符串字段,其中包含它的类别
模型的名称
当类别
模型的名称
更改时,指向该类别的每个子类别
应更新其类别
字段以匹配新名称
我目前直接在路由处理程序上实现这一点,方法是获取指向某个类别的所有子类别
,通过它们进行迭代并更新其类别字段
我想在模型中实现这个逻辑,它应该属于哪里
我在Category
模型上创建了一个预中间件
CategorySchema.pre('save', function(next) {
console.log('Saving ' + this.name);
next();
});
负责PUT
请求的路由处理程序使用更新后的名称保存模型,如下所示:
...
parentCategory.name = requestedName;
parentCategory.validate(function(validationError) {
if (validationError) {
return res.send(response.BAD_REQUEST);
}
parentCategory.save(function(saveError) {
if(saveError) {
return res.send(response.SERVER_ERROR);
}
});
});
...
CategorySchema.pre('save', function(next) {
var that = this;
var updateEachSubCategory = function(subcategory) {
subcategory.category = that.name;
subcategory.save(function(saveError) {
if(saveError) {
console.log(saveError);
return 1;
}
});
};
var updateChildrenCategoryField = function(err, subCategoriesArray) {
solange.asyncFor(subCategoriesArray, updateEachSubCategory);
};
var findChildren = function(categorySearchError, categoryFound) {
if(!categorySearchError) {
mongoose.models["SubCategory"].find({ category: categoryFound.name }, updateChildrenCategoryField);
}
};
mongoose.models["Category"].findOne({ _id: this._id }, findChildren);
next();
});
但是,我会打印新的名称,因此我无法迭代它的子类别,因为我只能访问新名称
我不确定如何将新旧名称传递给中间件,以便在中间件中执行操作。我可以通过如下方式查询类别模型来访问旧值:
...
parentCategory.name = requestedName;
parentCategory.validate(function(validationError) {
if (validationError) {
return res.send(response.BAD_REQUEST);
}
parentCategory.save(function(saveError) {
if(saveError) {
return res.send(response.SERVER_ERROR);
}
});
});
...
CategorySchema.pre('save', function(next) {
var that = this;
var updateEachSubCategory = function(subcategory) {
subcategory.category = that.name;
subcategory.save(function(saveError) {
if(saveError) {
console.log(saveError);
return 1;
}
});
};
var updateChildrenCategoryField = function(err, subCategoriesArray) {
solange.asyncFor(subCategoriesArray, updateEachSubCategory);
};
var findChildren = function(categorySearchError, categoryFound) {
if(!categorySearchError) {
mongoose.models["SubCategory"].find({ category: categoryFound.name }, updateChildrenCategoryField);
}
};
mongoose.models["Category"].findOne({ _id: this._id }, findChildren);
next();
});
由于这是一个之前的保存中间件,新名称尚未保存,因此我们可以通过查询模型来访问它。我的解决方案是备份初始化后中间件中的属性值
schema.post('init', function(model) {
model.oldValues = JSON.parse(JSON.stringify(model));
}
在属性修改之前,我们可以在任何其他中间件中访问这些属性,例如this.someAttr===this.oldValues.someAttr
我写了一封信
下面是它的输出:
------------------
Pre init attributes
The doc is new, run app once again
------------------
------------------
Post init attributes
{ _id: 55a37cc88ff0f55e6ee9be2d,
name: 'Test 1 - 12:07:88',
__v: 0,
ready: true }
Setting this.oldValues
------------------
------------------
Pre save attributes
{ _id: 55a37cc88ff0f55e6ee9be2d,
name: 'Test 1 - 12:07:16',
__v: 0,
ready: false }
------------------
------------------
Post save attributes
{ _id: 55a37cc88ff0f55e6ee9be2d,
name: 'Test 1 - 12:07:16',
__v: 0,
ready: false }
The old attributes (model.oldValues)
{ _id: '55a37cc88ff0f55e6ee9be2d',
name: 'Test 1 - 12:07:88',
__v: 0,
ready: true }
------------------
------------------
初始化前属性
该文档是新的,请再次运行应用程序
------------------
------------------
初始化后属性
{u id:55a37cc88ff0f55e6ee9be2d,
名称:“测试1-12:07:88”,
__v:0,
准备好了吗}
将此设置为.oldValues
------------------
------------------
预保存属性
{u id:55a37cc88ff0f55e6ee9be2d,
名称:“测试1-12:07:16”,
__v:0,
就绪:false}
------------------
------------------
保存后属性
{u id:55a37cc88ff0f55e6ee9be2d,
名称:“测试1-12:07:16”,
__v:0,
就绪:false}
旧属性(model.oldValues)
{u id:'55a37cc88ff0f55e6ee9be2d',
名称:“测试1-12:07:88”,
__v:0,
准备好了吗}
------------------