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

时间:2019-11-12 22:04:04

标签: node.js mongodb express mongoose mongoose-schema

我正在使用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,
        required: true,
        trim: true
    },
    authorName: {
        type: String,
        required: true,
        trim: true
    },
    authorSurname: {
        type: String,
        required: true,
        trim: true
    },
    language: {
        type: String,
        required: true,
        trim: true
    },
    type: {
        type: String,
        required: true,
        trim: true
    },
    coAuthors: {
        type: Array,
        default: []
    },
    isbn: {
        type: String,
        default: '',
        trim: true
    },
    genre: {
        type: String,
        default: '',
        trim: true
    },
    synopsis: {
        type: String,
        default: '',
        trim: true
    },
    publisher: {
        type: String,
        default: '',
        trim: true
    },
    location: {
        type: String,
        trim: true
    },
    readStatus: {
        type: String,
        default: 'Unspecified',
        trim: true
    },
    lent: {
        type: Boolean,
        default: false
    },
    lentTo: {
        type: String,
        default: '',
        trim: true
    },
    imageUrl: {
        type: String,
        default: 'https://image.png'
    },
    tags: {
        type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Tag' }],
        default: []
    },
    comments: {
        type: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
        default: []
    },
    likes: {
        type: Number,
        default: 0
    },
    suggested: {
        type: Boolean,
        default: false
    },
    favourite: {
        type: Boolean,
        default: false
    },
    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', async (book, next) => {
    // removes the book reference from the relative library
    await BookLibrary.findOneAndUpdate(
        {_id: book.libraryId},
        {$pull: {books: book._id}}
    )
})

BookSchema.post('findOneAndDelete', async (book, 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,
        required: true,
        trim: true
    },
    mediaId: {
        type: mongoose.Schema.Types.ObjectId,
        required: true
    },
    userHandle: {
        type: String,
        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导入,则一切正常,并且我无法理解其背后的原因。 我对编程很陌生,这是我的第一个项目,我尝试过在线查找,但不确定要看什么。

任何帮助将不胜感激。

0 个答案:

没有答案