Skip to content

Commit c794216

Browse files
author
Andreas Reiterer
committed
fix: single rel is undefined instead of {} when keyHere is null
1 parent ac48a58 commit c794216

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/shallow-populate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function getRelatedItems (ids, relatedItems, include, params) {
280280
const limit = _get(params, 'query.$limit', Math.max)
281281
ids = [].concat(ids || [])
282282
let skipped = 0
283-
let itemOrItems = (asArray) ? [] : {}
283+
let itemOrItems = (asArray) ? [] : undefined
284284

285285
let isDone = false
286286
for (let i = 0, n = relatedItems.length; i < n; i++) {

test/shallow-populate.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,43 @@ describe('populating thing', () => {
304304
}
305305
})
306306

307+
it('does nothing if keyHere of related item is null', async () => {
308+
for (const { type, dataResult } of beforeAfter) {
309+
const options = {
310+
include: {
311+
// from: 'users',
312+
service: 'posts',
313+
nameAs: 'posts',
314+
keyHere: 'postsId',
315+
keyThere: 'id',
316+
asArray: false
317+
}
318+
}
319+
const context = {
320+
app: {
321+
service (path) {
322+
return services[path]
323+
}
324+
},
325+
method: 'create',
326+
type,
327+
params: {},
328+
[dataResult]: {
329+
id: '11',
330+
name: 'Dumb Stuff',
331+
postsId: null
332+
}
333+
}
334+
335+
const shallowPopulate = makePopulate(options)
336+
337+
const response = await shallowPopulate(context)
338+
const result = response[dataResult]
339+
340+
assert(!result.posts, `${type}: post should have not been populated`)
341+
}
342+
})
343+
307344
it('populates from local keys dot notation', async () => {
308345
for (const { type, dataResult } of beforeAfter) {
309346
const options = {

0 commit comments

Comments
 (0)