gpt4 book ai didi

node.js - 由于外部模型, Hook (预中间件)中的 Mongoose 模型查询(findById)错误

转载 作者:可可西里 更新时间:2023-11-01 10:45:36 26 4
gpt4 key购买 nike

在这种情况下,我有两个模型:协议(protocol)和评论。每个模型都有一个调用另一个模型的中间件('pre' 或 'remove')。在 Comment.js 中调用 Comment 中间件会在 Protocol.findById() 处停止,因为 Protocol 是一个对象而不是模型函数。这是 Comment.js 中的 console.log(Protocol)

Protocol : [object Object] Type : object

const Comment = require('../models/comment') 在 Protocol.js 中被移除时,Comment.js 中间件工作,控制台将其中的 Protocol 视为关注:

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 中间件工作正常,尽管 Comment.js 有 const Protocol = require('../models/protocol')

我在 another topic 中发现了一个技巧,也就是把中间件Protocol.findById()里面替换成mongoose.model('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"
}
}

最佳答案

已经有一段时间了,到目前为止我还没有遇到同样的问题,所以我仍然没有明确的解决方案。但我现在看到一个可能导致这种无法解释的行为的错误。我在 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 Service 中:Elastic Beanstalk 阻止 API 编译。

这不是真正的答案,但我希望这可能有所帮助!

关于node.js - 由于外部模型, Hook (预中间件)中的 Mongoose 模型查询(findById)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52876293/

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