2016-06-28 78 views
0

您好我想创建通过猫鼬一个新的子文档,但我发现下面的消息时,我在邮差执行POST方法:消息:路径需要

{ 
    "message": "Location validation failed", 
    "name": "ValidationError", 
    "errors": { 
    "reviews.1.reviewText": { 
     "message": "Path `reviewText` is required.", 
     "name": "ValidatorError", 
     "properties": { 
     "type": "required", 
     "message": "Path `{PATH}` is required.", 
     "path": "reviewText" 
     }, 
     "kind": "required", 
     "path": "reviewText" 
    }, 
    "reviews.1.rating": { 
     "message": "Path `rating` is required.", 
     "name": "ValidatorError", 
     "properties": { 
     "type": "required", 
     "message": "Path `{PATH}` is required.", 
     "path": "rating" 
     }, 
     "kind": "required", 
     "path": "rating" 
    }, 
    "reviews.1.author": { 
     "message": "Path `author` is required.", 
     "name": "ValidatorError", 
     "properties": { 
     "type": "required", 
     "message": "Path `{PATH}` is required.", 
     "path": "author" 
     }, 
     "kind": "required", 
     "path": "author" 
    } 
    } 
} 

这里是我的DB模式为位置:

var mongoose = require('mongoose'); 

var reviewSchema = new mongoose.Schema({ 
    author: {type: String, required: true}, 
    rating: {type: Number, required: true, min: 0, max: 5}, 
    reviewText: {type: String, required: true}, 
    createdOn: {type: Date, "default": Date.now} 
}); 

var openingTimeSchema = new mongoose.Schema({ 
    days: {type: String, required: true}, 
    opening: String, 
    closing: String, 
    closed: {type: Boolean, required: true} 
}); 

var locationSchema = new mongoose.Schema({ 
    name: {type: String, required: true}, 
    address: String, 
    rating: {type: Number, "default":0, min: 0, max: 5}, 
    facilities: [String], 
    coords: {type: [Number], index:'2ndsphere'}, 
    openingTimes: [openingTimeSchema], 
    reviews: [reviewSchema] 
}); 

mongoose.model('Location', locationSchema); 

这里下router.post启动控制器( '/位置/:locationid /评论',ctrlReviews.reviewsCreate);路由:

//reviews.js 
var mongoose = require('mongoose'); 
var Loc = mongoose.model('Location'); 

module.exports.reviewsCreate = function (req, res) { 
    var locationid = req.params.locationid; 
    if(locationid){ 
     Loc 
      .findById(locationid) 
      .select('reviews') 
      .exec(
       function(err, location){ 
        if(err){ 
         sendJsonResponse(res, 400, err); 
        } else{ 
         console.log(location); 
         doAddReview(req, res, location); 
        } 
       } 
      ); 
    } else{ 
     sendJsonResponse(res, 400, { 
      "message" : "Not found, locationid required" 
     }); 
    } 
}; 
// START - Functions for review create ////////////////////////////////////// 
var doAddReview = function(req, res, location){ 
    if(!location){ 
     sendJsonResponse(res, 404, "locationid not found"); 
    } else{ 
     location.reviews.push({ 
      author: req.body.author, 
      rating: req.body.rating, 
      reviewText: req.body.reviewText 
     }); 

     location.save(function(err, location){ 
      var thisReview; 
      if(err){ 
       //sendJsonResponse(res, 400, err); 
       sendJsonResponse(res, 400, err); 
      } else{ 
       updateAverageRating(location._id); 
       thisReview = location.reviews[location.reviews.length - 1]; 
       sendJsonResponse(res, 201, thisReview); 
      } 
     }); 
    } 
}; 

var updateAverageRating = function(locationid){ 
    console.log("Update rating average for", locationid); 
    Loc 
     .findById(locationid) 
     .select('reviews') 
     .exec(
      function(err, location){ 
       if(!err){ 
        doSetAverageRating(location); 
       } 
      } 
     ); 
}; 

var doSetAverageRating = function(location){ 
    var i, reviewCount, ratingAverage, ratingTotal; 
    if(location.reviews && location.reviews.length > 0){ 
     reviewCount = location.reviews.length; 
     ratingTotal = 0; 
     for(i=0; i<reviewCount; i++){ 
      ratingTotal = ratingTotal + location.reviews[i].rating; 
     } 
     ratingAverage = parseInt(ratingTotal/reviewCount, 10); 
     location.rating = ratingAverage; 
     location.save(function(err){ 
      if(err){ 
       console.log(err); 
      } else{ 
       console.log("Average rating updated to", ratingAverage); 
      } 
     }); 
    } 
}; 

执行location.save功能时,我已经看到了这个错误弹出。我正在通过一本书学习MEAN Stack,因此您可以在此处下载本章的完整代码:https://github.com/simonholmes/getting-MEAN/tree/chapter-06

我试过从app_api替换我的locations.js和reviews.js文件的代码/ controllers文件夹,但在这一点上应用程序崩溃,我猜是因为其他文件需要更新。 所以我卡在那里。

有谁知道为什么会发生?

在此先感谢!

回答

0
Check your 

    req.body.author, 
    req.body.rating, 
    req.body.reviewText 


They must be coming as empty string 
2

我相信你的问题可能是body-parser没有配置。

尝试NPM安装体分析器,然后在你的主服务器文件的顶部导入:

bodyParser = require('body-parser'); 

最后,配置它以供使用。这将允许你使用x-www-form-urlencoded:

// Setting up basic middleware for all Express requests 
app.use(bodyParser.urlencoded({ extended: false })); // Parses urlencoded bodies 
app.use(bodyParser.json()); // Send JSON responses 
+0

有一个类似的问题发生在我身上,而沿着这个东西是什么解决了它。 –

+0

Joshua我已经配置了这种方式,但我并没有放弃这个问题来自一些分析错误,因为每次我安装一个模块时,我都会面临如下几个警告:“npm WARN EJSONPARSE无法解析json”,也许这是相关的,但我找不到任何解决方案(缓存清除不解决)。 – AtomicNation

0

其实。如果我更改为:作者:“大妈妈”,评分:5,评论文字:“Lorem ipsum dolor amet”它完美的作品,但是当我用邮差把它加入身体时,它似乎是空的,我认为它应该管用。我为此使用了x-www-form-urlencode,但尝试了所有其他选项。我想undestand如何在这里使用它...

0

似乎这是排序:我在review.push中添加了id创建功能和工作。真的,我仍然不知道为什么这是必要的,因为通常猫鼬将id添加到文档和子文档中,即使我遇到了其他控制器的问题,因为它添加了id的地方不应该在这里更新的代码doAddReview函数排序本期:

var doAddReview = function(req, res, location){ 
    if(!location){ 
     sendJsonResponse(res, 404, "locationid not found"); 
    } else{ 
     location.reviews.push({ 
      _id: mongoose.Types.ObjectId(), 
      author: req.body.author, 
      rating: req.body.rating, 
      reviewText: req.body.reviewText 
      /* 
      author: "Big Mama", 
      rating: 5, 
      reviewText: "Lorem ipsum dolor amet" 
      */ 
     }); 

     location.save(function(err, location){ 
      var thisReview; 
      if(err){ 
       //sendJsonResponse("Error Here"); 
       sendJsonResponse(res, 400, err); 
      } else{ 
       updateAverageRating(location._id); 
       thisReview = location.reviews[location.reviews.length - 1]; 
       sendJsonResponse(res, 201, thisReview); 
      } 
     }); 
    } 
}; 

非常感谢Joshua和Piyush!

1

他兄弟..检查这个图像..我也面临同样的问题..这就是我做错了什么。 enter image description here