私はSimon Holmesの著書「Getting Getting Mean」に従っていますが、私はサブ文書に関する問題を遭遇しましたids。 "MongooseDocumentArray.id(id) mongoose docsが提供するヘルパー関数を使用して" Location document "内の" review document "をリクエストしようとしていますが、私のすべてのサブドキュメントは" id "を生成しますが、ヌル「オブジェクトMongoose:文書の配列を検索するために "id"の代わりに "_id"を生成するサブ文書が必要です
機能の説明:。。一致する_idを持つ最初の文書を検索・アレイ・アイテム
は、ここでの問題は何である私の親文書であること? 『場所は_id』でそのIDを生成」 "、これはさらに私を困惑させます...入れ子になったスキーマの設定、レビューサブドキュメントとコントローラ関数foを含むデータベースから返されたロケーションオブジェクトfoクエリを実行するルートは、次のコードスニペットにそれぞれ示されています。次のスニペットについて
var mongoose = require("mongoose");
var reviewSchema = new mongoose.Schema({
author: String,
rating: {type: Number, required: true, min: 0, max: 5},
reviewText: String,
createdOn: {type: Date, "default": Date.now}
});
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: "2dsphere"},
reviews: [reviewSchema]
});
mongoose.model('Location', locationSchema);
Returned Location object showing subdocuments containing id instead of _id
、これは、関連する経路である:
router.get("/locations/:locationid/reviews/:reviewid", ctrlReviews.reviewsReadOne);
module.exports.reviewsReadOne = function(req, res){
if(req.params && req.params.locationid && req.params.reviewid){
Loc.findById(req.params.locationid)
.select("name reviews")
.exec(function(err, location) {
var response, review;
if(location.reviews && location.reviews.length > 0){
// This returns zero, because the function requires _id
// but all subdocuments have path/properties called id
review = location.reviews.id(req.params.reviewid);
console.log(review);
if(!review){
sendJsonResponse(res, 404, {
"message": "reviewid not found"
});
} else {
response = {
location: {
name: location.name,
id : req.params.locationid
},
review: review
};
sendJsonResponse(res, 200, response);
}
}
})
}
}。
データベース接続を修正するとすぐにこの解決方法を確認し、システムを完全に再インストールした後に再接続することをお勧めします。 – Peyno
それは動作しません。私はまだ同じ問題を抱えています。他のアイデアを感謝します、ありがとう – Peyno