基本上我只想在向REST API发出GET请求时才发送一些数据。我的GET请求代码:
router.get('/user/all', function(req, res, next){
User.find({name: req.query.name}).then(function(assets){
res.send(assets);
});
});
它返回:
[
{
"_id": "546b454b5634563b546",
"name": "John Doe",
"email": "johndoe@johndoe.com",
"phoneNo": "00000000000",
"active": true,
"__v": 0,
"assets": [
{
"name": "house",
"location": {
"_id": "592190ce29f12e179446d837",
"coordinates": [
-81.5,
24.1
],
"type": "point"
},
"_id": "592190ce29f12e179446d836"
}
]
}
]
但我希望它只返回:
[
{
"name": "house",
"location": {
"_id": "592190ce29f12e179446d837",
"coordinates": [
-81.5,
24.1
],
"type": "point"
},
"_id": "592190ce29f12e179446d836"
}
]
如何更改API请求以实现此目的?
由于
答案 0 :(得分: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;}))
});
});