2017-05-21 17 views
0

基本的には、REST APIにGETリクエストを送信するときに、一部のデータを返信したいだけです。 GETリクエストのための私のコード:GETリクエストで一部のデータのみを送信する

[ 
    { 
    "_id": "546b454b5634563b546", 
    "name": "John Doe", 
    "email": "[email protected]", 
    "phoneNo": "00000000000", 
    "active": true, 
    "__v": 0, 
    "assets": [ 
     { 
     "name": "house", 
     "location": { 
      "_id": "592190ce29f12e179446d837", 
      "coordinates": [ 
      -81.5, 
      24.1 
      ], 
      "type": "point" 
     }, 
     "_id": "592190ce29f12e179446d836" 
     } 
    ] 
    } 
] 

しかし、私はそれだけで返すようにしたい:

router.get('/user/all', function(req, res, next){ 
    User.find({name: req.query.name}).then(function(assets){ 
     res.send(assets); 
    }); 
}); 

それは返す

[ 
    { 
    "name": "house", 
    "location": { 
    "_id": "592190ce29f12e179446d837", 
    "coordinates": [ 
     -81.5, 
     24.1 
     ], 
     "type": "point" 
    }, 
    "_id": "592190ce29f12e179446d836" 
    } 
] 

がどのように私はこれを達成するためのAPIリクエストを変更できますか?

おかげ

答えて

1

あなただけの資産を返すようにしたい場合は、この方法を試してください。

router.get('/user/all', function(req, res, next){ 
    User.find({name: req.query.name}).then(function(assets){ 
     res.send(assets.map(function(x) {return x.assets;})) 
    }); 
}); 
+0

恐ろしいが、魔法のように働いていたが、あなたは男です! –

関連する問題