2016-06-28 71 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