diff --git a/lib/query/hypernova/assembler.js b/lib/query/hypernova/assembler.js index 91c6591..ffdcb59 100644 --- a/lib/query/hypernova/assembler.js +++ b/lib/query/hypernova/assembler.js @@ -3,6 +3,10 @@ import cleanObjectForMetaFilters from './lib/cleanObjectForMetaFilters'; import sift from 'sift'; export default (childCollectionNode, { limit, skip, metaFilters }) => { + if (childCollectionNode.results.length === 0) { + return; + } + const parent = childCollectionNode.parent; const linker = childCollectionNode.linker; @@ -28,6 +32,10 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => { if (strategy === 'one') { parent.results.forEach(parentResult => { + if (!parentResult[fieldStorage]) { + return; + } + parentResult[childCollectionNode.linkName] = filterAssembledData( resultsByKeyId[parentResult[fieldStorage]], { limit, skip } @@ -37,9 +45,13 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => { if (strategy === 'many') { parent.results.forEach(parentResult => { + if (!parentResult[fieldStorage]) { + return; + } + let data = []; parentResult[fieldStorage].forEach(_id => { - data.push(resultsByKeyId[_id]); + data.push(_.first(resultsByKeyId[_id])); }); parentResult[childCollectionNode.linkName] = filterAssembledData( @@ -51,6 +63,10 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => { if (strategy === 'one-meta') { parent.results.forEach(parentResult => { + if (!parentResult[fieldStorage]) { + return; + } + const _id = parentResult[fieldStorage]._id; parentResult[childCollectionNode.linkName] = filterAssembledData( resultsByKeyId[_id], @@ -64,7 +80,7 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => { const _ids = _.pluck(parentResult[fieldStorage], '_id'); let data = []; _ids.forEach(_id => { - data.push(resultsByKeyId[_id]); + data.push(_.first(resultsByKeyId[_id])); }); parentResult[childCollectionNode.linkName] = filterAssembledData( @@ -76,7 +92,7 @@ export default (childCollectionNode, { limit, skip, metaFilters }) => { }; function filterAssembledData(data, { limit, skip }) { - if (limit) { + if (limit && Array.isArray(data)) { return data.slice(skip, limit); } diff --git a/lib/query/testing/link-cache/server.test.js b/lib/query/testing/link-cache/server.test.js index d49949a..cc60716 100755 --- a/lib/query/testing/link-cache/server.test.js +++ b/lib/query/testing/link-cache/server.test.js @@ -9,8 +9,9 @@ import { } from './collections'; describe('Query Link Denormalization', function() { - before(() => { + before((done) => { createFixtures(); + done(); }); it('Should not cache work with nested options', function() {