gpt4 book ai didi

node.js - 消息: path is required

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:59 25 4
gpt4 key购买 nike

您好,我正在尝试通过 mongoose 创建一个新的子文档,但是当我在 Postman 中执行 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"
}
}
}

这是我的位置数据库架构:

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);

这里 Controller 在 router.post('/locations/:locationid/reviews', 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/controllers文件夹中的locations.js和reviews.js文件的代码,但此时应用程序崩溃了,我猜是因为其他文件需要更新。所以我被困在那里。

有人知道为什么会发生这种情况吗?

提前致谢!

最佳答案

他兄弟..检查这张图片..我也面临着同样的问题..这就是我做错的事情。 enter image description here

关于node.js - 消息: path is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com