mirror of
https://github.com/typicode/json-server.git
synced 2025-07-28 12:43:18 +08:00
Fix _embed
This commit is contained in:
@ -145,8 +145,9 @@ module.exports = function (source) {
|
|||||||
// GET /:resource/:id
|
// GET /:resource/:id
|
||||||
function show (req, res, next) {
|
function show (req, res, next) {
|
||||||
var _embed = req.query._embed
|
var _embed = req.query._embed
|
||||||
|
var id = utils.toNative(req.params.id)
|
||||||
var resource = db(req.params.resource)
|
var resource = db(req.params.resource)
|
||||||
.get(utils.toNative(req.params.id))
|
.get(id)
|
||||||
|
|
||||||
if (resource) {
|
if (resource) {
|
||||||
// Clone resource to avoid making changes to the underlying object
|
// Clone resource to avoid making changes to the underlying object
|
||||||
@ -156,12 +157,15 @@ module.exports = function (source) {
|
|||||||
|
|
||||||
// Embed other resources based on resource id
|
// Embed other resources based on resource id
|
||||||
_embed.forEach(function (otherResource) {
|
_embed.forEach(function (otherResource) {
|
||||||
if (otherResource && otherResource.trim().length > 0) {
|
|
||||||
// Skip non-existent collections
|
if (otherResource
|
||||||
if (!db.object[otherResource]) return
|
&& otherResource.trim().length > 0
|
||||||
|
&& db.object[otherResource]) {
|
||||||
var query = {}
|
var query = {}
|
||||||
query[req.params.resource + 'Id'] = req.params.id
|
var prop = pluralize.singular(req.params.resource) + 'Id'
|
||||||
|
query[prop] = id
|
||||||
resource[otherResource] = db(otherResource).where(query)
|
resource[otherResource] = db(otherResource).where(query)
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user