【问题标题】:Still getting "Schema is not a constructor", despite properly importing and exporting my Schema尽管正确导入和导出我的架构,但仍然得到“架构不是构造函数”
【发布时间】:2017-07-01 06:36:14
【问题描述】:

这是我的代码。我在一个名为“review.js”的单独文件中有一个审查架构。

const express = require('express');
const mongoose = require('mongoose');
const User = require('../model/user');
require('mongoose-currency').loadType(mongoose);
const Currency = mongoose.Types.Currency;
const Schema = mongoose.Schema;

let reviewSchema = new Schema();

reviewSchema.add({


    rating: {
        type: Number,
        min: 1,
        max: 5,
        defualt: 0
    },


    howEasyToMake: {
        type: Number,
        required: true,
        min: 1,
        max: 5
    },

    howGoodTaste: {
        type: Number,
        required: true,
        min: 1,
        max: 5,
    },

    wouldMakeAgain: {
        type: Number,
        required: true,
        min: 1,
        max: 5,
    },


    comment: {
        type: String,
        default: ""
    },


    postedBy: {
        type: String,
        required: true,
        index: true
    },

    reviewOf: {
        type: String,
        required: true,
        index: true
    },

    postersCreationDate: {
        type: Number,
        required: true
    },

    chefsCreationDate: {
        type: Number,
        required: true
    },

    chefsId: {
        type: String,
        required: true
    }


});

module.exports.reviewSchema = reviewSchema;

我有另一个名为 recipe.js 的文件,我在其中导入 reviewSchema 并将其用作我的 Recipe 模型架构的嵌入式架构。

const express = require('express');
const mongoose = require('mongoose');
const User = require('../model/user');
require('mongoose-currency').loadType(mongoose);
const Currency = mongoose.Types.Currency;
const Schema = mongoose.Schema;
const reviewSchema = require('../model/review').reviewSchema;



let recipeSchema = new Schema({

  name: {
    type: String,
    required: true
  },

  description: {
    type: String,
  },

  steps: {
    type: String,
    required: true,
  },

  ingredients: {
    type: Array,
    default: ['1', '2', '3', '4']
  },

  category: {
    type: String,
    required: true,
    index: true
  },

  postedBy: {
    type: String,
    required: true,
  },


  reviewsOfRecipe: [reviewSchema],

  numberOfRatings: {
    type: Number,
    default: 0
  },

  totalAddedRatings: {
      type: Number,
      default: 0
  },

  reviewAverage: {
      type: Number,
      default: undefined
  },



  postersCreationDate: {
    type: Number,
    index: true
  },

  likedBy: {
      type: Array

  },

  reviewedBy: {
      type: Array
  }


});



recipeSchema.methods.updateReviewAverage = function(){

    let recipe = this;

    this.reviewAverage = this.totalAddedRatings / this.numberOfRatings;

};


let Recipe = mongoose.model('Recipe', recipeSchema);

module.exports = Recipe;

我有另一个名为recipeRouter.js 的文件,我在其中使用reviewSchema 构建评论,然后将其插入到我的Recipe 文档中嵌入的reviewsOfRecipes 数组中。在我的 recipRouter.js 文件中,每次我的代码尝试执行此操作时......

Recipe.findOne({name: req.params.name}).then((recipe) => {

   let review_ = new reviewSchema({
            comment: req.body.comment
            rating: req.body.score
        });

   recipe.reviewsOfRecipe.push(review_);

})

...我收到此错误。

 TypeError: reviewSchema is not a constructor

以前,当我遇到这个问题时,我将 reviewSchema 与我的 Recipe 模型架构放在同一个文件中。从那时起,我将两者分成了各自的文件。我确保在我的review.js 文件中正确导出module.exports.reviewSchema = reviewSchema;,并确保在我的recipe.jsrecipeRouter.js 文件中都有const reviewSchema = require('../model/review').reviewSchema;。然而,这个问题仍然存在/如果有人能指出可能是什么问题,我将不胜感激。

【问题讨论】:

  • 您不会创建这样的实例。而是做recipe.reviewsOfRecipe.push({ comment: req.body.comment, rating: req.body.score })。请参阅 mongoose 文档页面底部的sub-docs。更好的是,您应该了解$push 运算符允许您添加到服务器上现有文档的数组中,而不是检索文档并添加到数组。

标签: javascript node.js mongoose mongoose-schema


【解决方案1】:

您必须像对另一个架构所做的那样导出reviewSchema(使用mongoose.model):

module.exports.reviewSchema = mongoose.model('Review', reviewSchema);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-15
    • 2019-10-16
    • 2020-03-22
    • 2022-06-11
    • 1970-01-01
    • 2018-03-19
    • 2021-09-09
    • 2022-01-23
    相关资源
    最近更新 更多