我一直得到CastError:Cast为数字失败的值" NaN"

时间:2017-07-03 19:00:36

标签: javascript node.js mongodb 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
    }


});

module.exports = reviewSchema;

这里是recipe.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;
const reviewSchema = require('../model/review');



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: 0
  },



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

  likedBy: {
      type: Array

  },

  reviewedBy: {
      type: Array
  }


});


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

module.exports = Recipe;

recipe.js我明显有numberOfRatingsratingtotalAddedRatings.js作为数字。我对wouldMakeAgain中的howEasyToMakehowGoodTastereview.js执行相同操作。

当有人提交评论时,每当我尝试更新我的食谱文档时都会出现问题。

这些特定的代码行似乎不起作用。我应该注意,在这段代码中,recipe指的是我从Recipe.findOne({name:req.params.name})获得的配方的模型实例。当我在这部分代码中使用console.log(配方)时,会打印出配方。所以我有模型实例,问题是我无法更新它。以下是不合适的行似乎不起作用。

recipe.update({$inc: {numberOfRatings: 1}});


                recipe.save(function (err) {
                    if (err) return handleError(err);
                    console.log('Success!');
                });

            recipe.update({$inc: {totalAddedRatings: reviewScore}});

            recipe.save(function (err) {
                if (err) return handleError(err);
                console.log('Success!');
            });

numberOfRatingtotalAddedRatings没有像预期的那样增加。

在这里,我尝试计算配方的新评价平均值......

let newAverage = Number(req.body.totalAddedRatings) / Number(req.body.numberOfRatings);

当我console.log(typeof(newAverage));时,我得到number。所以JS告诉我它是一个数字。但是,当我尝试这样做时,

        recipe.update({$set: {reviewAverage: newAverage}});

        recipe.save(function (err) {
            if (err) return handleError(err);
            console.log('Success!');
        });

我收到以下错误:

(node:2352) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CastError
: Cast to number failed for value "NaN" at path "reviewAverage"

任何人都可以向我解释我做错了什么吗?我不应该将$set与数字一起使用吗?

更新:我试图做一些改变以期解决这个问题,但我得到了相同的结果。

我试着这样做......

        let newAverage = Number(req.body.totalAddedRatings) / Number(req.body.numberOfRatings);

        let numAverage = Number(newAverage);

  recipe.update({$set: {reviewAverage: numAverage}});

        recipe.save(function (err) {
            if (err) return handleError(err);
            console.log('Success!');
        });

但是这样做给了我同样的错误信息。当我console.log(typeof(numAverage));时,它打印出number。但奇怪的是,当我console.log(numAverage));时,我得到NaN

我也试过这样做,但仍然有错误:

recipe.set('reviewAverage', numAverage);

0 个答案:

没有答案