猫鼬-无法识别另一种模型

我正在使用mongoDB和mongoose编写一个小型节点/ express应用程序。这是一个图书图书馆应用程序,我的两个模型有问题:“图书”和“评论”。 当用户添加新注释时,将创建一个Comment对象,然后将文档的ID添加到Book文档内的数组(“ comments”)中。

一切正常,直到我决定在Book中添加后期中间件,以便在删除图书时删除所有图书注释。在Book模式文件中导入Comment模型后,添加注释的请求停止工作,并给我以下错误:

TypeError: Book.findOneAndUpdate is not a function
    at model.<anonymous> (/appDir/app/dbModels/media/interactions/Comment.js:29:16)
    at next (/appDir/node_modules/kareem/index.js:198:31)
    at Kareem.execPost (/appDir/node_modules/kareem/index.js:217:3)
    at _cb (/appDir/node_modules/kareem/index.js:307:15)
    at /appDir/node_modules/mongoose/lib/model.js:385:5
    at /appDir/node_modules/mongoose/lib/model.js:274:7
    at /appDir/node_modules/mongodb/lib/operations/execute_operation.js:75:17
    at /appDir/node_modules/mongodb/lib/operations/execute_operation.js:64:11
    at ClientSession.endSession (/appDir/node_modules/mongodb/lib/core/sessions.js:135:41)
    at executeCallback (/appDir/node_modules/mongodb/lib/operations/execute_operation.js:59:17)
    at /appDir/node_modules/mongodb/lib/operations/insert_one.js:34:21
    at handleCallback (/appDir/node_modules/mongodb/lib/utils.js:129:55)
    at /appDir/node_modules/mongodb/lib/operations/common_functions.js:270:5
    at handler (/appDir/node_modules/mongodb/lib/core/sdam/topology.js:1000:24)
    at /appDir/node_modules/mongodb/lib/core/sdam/server.js:457:5
    at /appDir/node_modules/mongodb/lib/core/connection/pool.js:408:18

这是我的Book.js文件:

const mongoose = require('mongoose')

/* BOOK SCHEMA */
const BookSchema = new mongoose.Schema({
    libraryId: {
        type: String,required: true,trim: true
    },title: {
        type: String,authorName: {
        type: String,authorSurname: {
        type: String,language: {
        type: String,type: {
        type: String,coAuthors: {
        type: Array,default: []
    },isbn: {
        type: String,default: '',genre: {
        type: String,synopsis: {
        type: String,publisher: {
        type: String,location: {
        type: String,readStatus: {
        type: String,default: 'Unspecified',lent: {
        type: Boolean,default: false
    },lentTo: {
        type: String,imageUrl: {
        type: String,default: 'https://image.png'
    },tags: {
        type: [{ type: mongoose.Schema.Types.ObjectId,ref: 'Tag' }],comments: {
        type: [{ type: mongoose.Schema.Types.ObjectId,ref: 'Comment' }],likes: {
        type: Number,default: 0
    },suggested: {
        type: Boolean,favourite: {
        type: Boolean,userHandle: {
        type: String,required: true
    }
})

/* MIDDLEWARES */
const Booklibrary = require('../medialibrary/Booklibrary')
const Comment = require('./interactions/Comment')
const User = require('../user/User')
const deleteOldImage = require('../../services/aws/s3/delete')

/* POST - SAVE */
BookSchema.post('save',async (book) => {
    // pushes the book to the relative library
    await Booklibrary.findOneAndUpdate(
        {_id: book.libraryId},{$push: {books: book._id}}
    )
})

/* POST - DELETE */
BookSchema.post('findOneAndDelete',async (book,next) => {
    // deletes the book cover from the database
    const deleted = deleteOldImage(book.imageUrl)
    if (!deleted) next(new Error('Error while deleting image from database'))
})

BookSchema.post('findOneAndDelete',next) => {
    // removes the book reference from the relative library
    await Booklibrary.findOneAndUpdate(
        {_id: book.libraryId},{$pull: {books: book._id}}
    )
})

BookSchema.post('findOneAndDelete',next) => {
    // deletes the book comments from the database
    await Comment.deleteMany({mediaId: book._id})
})

module.exports = mongoose.model('Book',BookSchema)

这是我的Comment.js文件

const mongoose = require('mongoose')

/* COMMENT SCHEMA */
const CommentSchema = mongoose.Schema({
    comment: {
        type: String,mediaId: {
        type: mongoose.Schema.Types.ObjectId,required: true
    },required: true
    }
})

/* MIDDLEWARES */
const Book = require('../Book')

/* POST-SAVE */
CommentSchema.post('save',async (comment) => {
    // pushes the comment to the relative Book
    await Book.findOneAndUpdate(
        {_id: comment.mediaId},{$push: {comments: comment._id}}
    )
})

module.exports = mongoose.model('Comment',CommentSchema)

如果我注释掉Book.js文件中的Comment导入,则一切正常,并且我无法理解其背后的原因。 我对编程很陌生,这是我的第一个项目,我尝试过在线查找,但不确定要看什么。

任何帮助将不胜感激。

ping00000 回答:猫鼬-无法识别另一种模型

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3114025.html

大家都在问