尽管正确导入和导出我的架构

时间:2017-06-30 17:24:29

标签: javascript node.js mongoose mongoose-schema

这是我的代码。我在一个名为" 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构建评论,然后将其插入我的reviewsOfRecipes文档中的嵌入式Recipe数组中。在我的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模型架构相同的文件中。从那以后,我把两个分成每个都有自己的文件。我确保在module.exports.reviewSchema = reviewSchema;文件中正确导出review.js,并确保在const reviewSchema = require('../model/review').reviewSchema;recipe.js文件中都有recipeRouter.js。然而,这个问题仍然存在/我会非常感激,如果有人能够指出可能是什么问题。

1 个答案:

答案 0 :(得分:1)

您必须像对待其他架构一样导出reviewSchema(使用mongoose.model):

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