gpt4 book ai didi

node.js - Mongoose 预保存 Hook 正在触发,但未保存其他字段(不使用 model.update)

转载 作者:可可西里 更新时间:2023-11-01 09:26:17 25 4
gpt4 key购买 nike

我试图在我的模式中实现一个计数器来获取下一个问题编号。我已经将它实现为 Mongoose 中的钩子(Hook)预保存钩子(Hook),一切看起来都很好......除了实际的“数字”字段不会更新。我可以很容易地通过记录到控制台的内容来判断 Hook 正在触发,甚至该字段似乎也已分配。但遗憾的是,无论我尝试什么,“数字”字段都不会出现在结果中。

我看到了几个与 Mongoose Hook 相关的问题,但它们似乎都与我没有使用的 findOneAndUpdate 等相关。

这是我的完整模型,底部有钩子(Hook):

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Project = require('./projects.js');

var IssueSchema = new Schema({
title: {type: String, required: true, trim: true, index: true},
number: {type: Number},
description: {type: String, required: true},
vote_up: {type: Number, default: 0},
vote_down: {type: Number, default: 0},
comments: [new Schema({
_id: {type: Schema.ObjectId, ref: 'users'},
description : {type: String},
likes: {type: Number},
date: {type: Date}
})],
attachments: [],
fields: [new Schema({
_id: {type: Schema.ObjectId, ref: 'fields'},
value : {type: String}
})],
project: {type: Schema.ObjectId, required: true, index: true, ref: 'projects'},
tags: {type: [Schema.ObjectId], required: false, default: ['56a2a0b1ea805d403f6d014b']},
isResolved: {type: Boolean, default: false},
created_by: {type: Schema.ObjectId, required: true, ref: 'users'},
updated_by: {type: Schema.ObjectId, required: false, ref: 'users'},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});

IssueSchema.pre('save', function(next){
var now = new Date();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now;
}
next();
})
.pre('save', function(next) {
Project.findOne({_id: this.project}).select('numberSeq').exec(function(err, doc) {
if (err) {
console.log(err);
}
console.log('pre-save hook firing');
this.number = doc.numberSeq;
console.log(this.number);
next();
});
})
.post('save', function(doc) {
Project.update({_id: doc.project}, {$inc: {numberSeq: 1}}, function(err, result) {
if (err) {
console.log(err);
}
console.log('Updated next number in seq for ' + doc.project);
});
});

module.exports = mongoose.model('issues', IssueSchema);

以及插入问题的路径(我猜这不是问题所在)

app.post('/api/issue/create', function(req, res) {

var issue = new Issues({
title: req.body.title,
description: req.body.description,
fields: req.body.fields,
attachments: req.body.attachments,
project: req.body.project,
created_by: req.user || req.body.created_by,
});

issue.save(function(err, result) {
if (err) {
return res.status(409).send({message: 'There was an error creating the issue: ' + err});
}
if (!result.number) {
console.log('number = :(');
}
console.log(result);
res.send({message: 'New issue created', result: result});
});
});

Screenshot of result from console with logging

最佳答案

你在这里错过了 this 上下文,

.pre('save', function(next) {
Project.findOne({_id: this.project}).select('numberSeq').exec(function(err, doc) {
if (err) {
console.log(err);
}
console.log('pre-save hook firing');
this.number = doc.numberSeq;
console.log(this.number);
next();
});
})

你在哪里说:

this.number = doc.numberSeq;
console.log(this.number);

实际上是指 findOne 查询的回调函数,因此您最终会得到正确的 console.log 和插入的错误数据。

您可以记住预保存 Hook 的这个上下文,然后在回调中使用该上下文更新数字。如下所示:

.pre('save', function(next) {
var tat=this;
Project.findOne({_id: this.project}).select('numberSeq').exec(function(err, doc) {
if (err) {
console.log(err);
}
console.log('pre-save hook firing');
tat.number = doc.numberSeq;
console.log(tat.number);
next();
});
})

关于node.js - Mongoose 预保存 Hook 正在触发,但未保存其他字段(不使用 model.update),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com