Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仍在继续",;架构不是构造函数";,尽管正确地导入和导出了我的模式_Javascript_Node.js_Mongoose_Mongoose Schema - Fatal编程技术网

Javascript 仍在继续",;架构不是构造函数";,尽管正确地导入和导出了我的模式

Javascript 仍在继续",;架构不是构造函数";,尽管正确地导入和导出了我的模式,javascript,node.js,mongoose,mongoose-schema,Javascript,Node.js,Mongoose,Mongoose Schema,这是我的密码。我在一个名为“review.js”的单独文件中有一个review模式 我有另一个名为recipe.js的文件,我在其中导入reviewSchema并将其用作我的配方模型模式的嵌入式模式 const express = require('express'); const mongoose = require('mongoose'); const User = require('../model/user'); require('mongoose-currency').loadType

这是我的密码。我在一个名为“review.js”的单独文件中有一个review模式

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

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
构建一个评论,然后将其插入我的
配方
文档中嵌入的
ReviewOfRecipes
数组中。在我的recipRouter.js文件中,每当我的代码尝试这样做时

Recipe.findOne({name:req.params.name})。然后((Recipe)=>{

})

…我得到了这个错误

 TypeError: reviewSchema is not a constructor

以前,当我遇到这个问题时,我将reviewSchema与我的配方模型模式放在同一个文件中。从那时起,我将这两个文件分成了各自的文件。我确保正确导出
module.exports.reviewSchema=reviewSchema
在我的
review.js
文件中,并确保有
const reviewSchema=require('../model/review').reviewSchema
在我的
recipe.js
recipeRouter.js
文件中。然而,这个问题还是出现了/如果有人能指出问题所在,我将不胜感激。

您必须像导出其他模式一样导出
reviewSchema
(使用
mongoose.model
):


您不能创建这样的实例。相反,执行
recipe.reviewOfRecipe.push({comment:req.body.comment,rating:req.body.score})
。请参阅mongoose文档中的页面底部。。更好的是,您应该了解到操作员允许您添加到服务器上现有文档的数组中,而不是检索文档并添加到数组中。
   let review_ = new reviewSchema({
            comment: req.body.comment
            rating: req.body.score
        });

   recipe.reviewsOfRecipe.push(review_);
 TypeError: reviewSchema is not a constructor
module.exports.reviewSchema = mongoose.model('Review', reviewSchema);