我真的是 Node 新手,但我目前有一个 NodeJS / Express 开源 CMS,并且想为我正在工作的应用程序输出一些 API 数据。如果我没有使用正确的术语或诸如此类的东西,请原谅我,这对我来说是新的。
我目前拥有的是两个系列,地点和旅游。CMS 允许我在两者之间建立关系。这只是将 ObjectID 的数组存储在每个关联的游览记录的位置记录中。
我想要做的是获取我的 API 输出代码(如下)并让它输出整个 tours 数组,并在每个位置记录中完成所有字段(标题、描述等)。目前它只输出一个 ID 数组。
这是我当前的代码:
var async = require('async'),
landmark = require('keystone');
var Location = keystone.list('Location'),
Tour = keystone.list('Tour');
/**
* List Locations
*/
exports.list = function(req, res) {
Location.model.find(function(err, items) {
if (err) return res.apiError('database error', err);
res.apiResponse({
locations: items
});
});
}
/**
* Get Location by ID
*/
exports.get = function(req, res) {
Location.model.findById(req.params.id).exec(function(err, item) {
if (err) return res.apiError('database error', err);
if (!item) return res.apiError('not found');
res.apiResponse({
location: item
});
});
}
当前 API 输出(截断):
{
"locations": [
{
"_id": "53a47997ebe91d8a4a26d251",
"slug": "test-location",
"lastModified": "2014-06-20T20:19:14.484Z",
"commonName": "test location",
"__v": 3,
"url": "",
"tours": [
"53a47963ebe91d8a4a26d250"
],
"images": []
}
]
}
我在找什么:
{
"locations": [
{
"_id": "53a47997ebe91d8a4a26d251",
"slug": "test-location",
"lastModified": "2014-06-20T20:19:14.484Z",
"commonName": "test location",
"__v": 3,
"url": "",
"tours": [
{
"_id": "53a47963ebe91d8a4a26d250",
"title": "my test tour title",
"url": "url_to_audio_file"
}
],
"images": []
}
]
}
有谁知道这是否可能?任何帮助,将不胜感激!谢谢!