在本例中,我有两个模型:协议和注释。每个模型都有一个中间件(‘pre’或'remove')来调用另一个模型。在Comment.js中调用注释中间件在Protocol.findById()停止,因为协议是一个对象而不是模型函数。以下是console.log(协议)在Comment.js中的应用
Protocol : [object Object] Type : object当const Comment = require('../models/comment')在Protocol.js中被删除时,Comment.js中间件就可以工作,控制台可以看到其中的协议如下:
Protocol : function model(doc, fields, skipId) {
model.hooks.execPreSync('createModel', doc);
if (!(this instanceof model)) {
return new model(doc, fields, skipId);
}
Model.call(this, doc, fields, skipId);
} Type : function我对这种行为不以为然。此外,另一方面,尽管Protocol.js有const Protocol = require('../models/protocol'),但Comment.js中间件工作得很好。
我在another topic中发现了一个窍门,就是用mongoose.model('Protocol').findById()替换中间件Protocol.findById()内部,但这并不能解释这个问题。
脚本如下所示。如果您需要更多的信息,请告诉我,我会提供的。谢谢
Protocol.js模型与中间件
// Dependencies
const mongoose = require('mongoose')
//Models
//<!---- If Comment is not required here then the Comment Middleware works ---->!
const Comment = require('../models/comment')
//Schema
const protocolSchema = mongoose.Schema({
_id : mongoose.Schema.Types.ObjectId,
title: {
type : String,
required: true
},
comments : [{
type: mongoose.Schema.Types.ObjectId,
ref : 'Comment',
}]
})
//Middleware Hook Call
protocolSchema.pre('remove', async function() {
console.log('Starts Protocol.schema.pre(\'remove\')')
var toBeDeletedProtocol = this
await removeComments()
function removeComments(){
return new Promise ((resolve, reject) => {
console.log('Starts removeComments()')
var deletedComments = []
Comment.find({protocol : toBeDeletedProtocol._id})
.exec()
.then( comments => {
console.log('comments found: ' + comments)
return resolve()
})
.catch(err => {
console.log('Removing comment(s) related to the deleted protocol failed in protocol remove Hook')
return reject(err)
})
})
}
})
//Model Export
module.exports = mongoose.model('Protocol', protocolSchema)Comment.js模型与中间件
//Dependencies
const mongoose = require('mongoose')
//Models
const Protocol = require('../models/protocol')
//Schema
const commentSchema = mongoose.Schema(
{
_id : mongoose.Schema.Types.ObjectId,
content: {
type: String,
required : true
},
protocol : {
type: mongoose.Schema.Types.ObjectId,
ref : 'Protocol',
required : true,
}
}
)
//Middleware Hook Call
commentSchema.pre('save', async function() {
console.log('Starts Comment.schema.pre(\'save\')')
var toBeSavedComment = this
await updateProtocol()
function updateProtocol(){
return new Promise ((resolve, reject) => {
console.log('Starts updateProtocol()')
console.log('toBeSavedComment : '+ toBeSavedComment)
console.log('Protocol : '+ Protocol, 'Type : ' + typeof Protocol)
//<!----- the ERROR occurs here -----!>
Protocol.findById(toBeSavedComment.protocol)
//<!----- Protocol is seen an object instead of a model function -----!>
.exec()
.then( protocol => {
console.log('protocol found : ' + protocol)
return resolve()
})
.catch(err => {
console.log('Error in updateProtocol() in Comment.schema pre \'save\'')
console.log(err)
return reject(err)
})
})
}
})
//Export
module.exports = mongoose.model('Comment', commentSchema)
Package.json
{
"name": "debug",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.3.2",
"nodemon": "^1.18.4"
}
}发布于 2019-05-14 10:13:05
这已经有一段时间了,到目前为止我还没有面对同样的问题,所以我仍然没有一个明确的解决方案。但我现在看到一个错误,可能会导致这种无法解释的行为。我在console.log中连接变量或对象,例如:
console.log('toBeSavedComment : '+ toBeSavedComment)
console.log('Protocol : '+ Protocol, 'Type : ' + typeof Protocol)相反,请使用逗号",“:
console.log('toBeSavedComment : ', toBeSavedComment)
console.log('Protocol : ', Protocol, 'Type : ' , typeof Protocol)对象连接在console.log中,例如,在Amazone Web中:弹力串连防止API编译。
这不是真正的答案,但我希望这可能会有所帮助!
https://stackoverflow.com/questions/52876293
复制相似问题