私は Node を初めて使用しますが、現在 NodeJS / Express オープン ソース CMS を使用しており、作業中のアプリの API データを出力したいと考えています。正しい用語を使用していない場合はご容赦ください。これは私にとって新しいことです。
私が現在持っているのは、2 つのコレクション、場所、およびツアーです。CMS を使用すると、2 つの間の関係を作成できます。これは、関連付けられた各ツアー レコードの場所レコードに 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": []
}
]
}
これが可能かどうか知っている人はいますか?どんな助けでも大歓迎です!ありがとう!