javascript初心者です。
nodejsの処理でmongodbから値と取り出す処理です。
下記のjsonデータの中でcommentsは配列になっておりますが、
この配列から指定した_idのデータを取り出す処理ですが、
id(req.params.commentId)で取得できるのですが、
なぜかわかりません。mongooseの機能でしょうか?
自己解決しました。
http://mongoosejs.com/docs/api.html#types_documentarray_MongooseDocumentArray.id
javascript
1Dishes.findById(req.params.dishId).populate('comments.postedBy').exec(function(err, dish) { 2 if(err) throw err; 3 res.json(dish.comments.id(req.params.commentId)); 4})
json
1{ 2 "_id": "58d6382b77fe4c0e9c3623f2", 3 "updatedAt": "2017-03-25T09:59:17.867Z", 4 "createdAt": "2017-03-25T09:28:11.185Z", 5 "name": "Uthapizza", 6 "image": "images/uthapizza.png", 7 "label": "Hot", 8 "price": 499, 9 "description": "A unique combination", 10 "__v": 4, 11 "comments": [ 12 { 13 "updatedAt": "2017-03-25T09:28:11.184Z", 14 "createdAt": "2017-03-25T09:28:11.184Z", 15 "rating": 5, 16 "comment": "Imagine all the eatables, living in conFusion!", 17 "_id": "58d6382b77fe4c0e9c3623f3", 18 "postedBy": null 19 }, 20 { 21 "updatedAt": "2017-03-25T09:58:04.110Z", 22 "createdAt": "2017-03-25T09:58:04.110Z", 23 "rating": 2, 24 "comment": "qqqqqqqqqqqqqqqq!", 25 "postedBy": { 26 "_id": "58d5e18c77fe4c0e9c3623f1", 27 "username": "fff", 28 "__v": 0, 29 "admin": true, 30 "lastname": "kakiku", 31 "firstname": "aiueo" 32 }, 33 "_id": "58d63f2c0713cd3dccfd4458" 34 }, 35 { 36 "updatedAt": "2017-03-25T09:59:17.867Z", 37 "createdAt": "2017-03-25T09:59:17.867Z", 38 "rating": 3, 39 "comment": "eeeeeeee!", 40 "postedBy": { 41 "_id": "58d5e15f2835921284b8af0d", 42 "username": "eee", 43 "__v": 0, 44 "admin": false, 45 "lastname": "", 46 "firstname": "" 47 }, 48 "_id": "58d63f750713cd3dccfd4459" 49 } 50 ], 51 "category": "mains" 52}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。