Skip to content

Commit 7c70c1e

Browse files
authored
Prevent crashing when no items provided (#141)
1 parent 510b158 commit 7c70c1e

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/helpers.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ export const index = function (items, fields) {
247247

248248
let i = 1;
249249

250+
fields.forEach((field) => {
251+
facets['data'][field] = Object.create(null);
252+
});
253+
250254
items && items.map((item) => {
251255
if (!item['_id']) {
252256
item['_id'] = i;
@@ -258,15 +262,10 @@ export const index = function (items, fields) {
258262

259263
items && items.map((item) => {
260264
fields.forEach((field) => {
261-
//if (!item || !item[field]) {
262265
if (!item) {
263266
return;
264267
}
265268

266-
if (!facets['data'][field]) {
267-
facets['data'][field] = Object.create(null);
268-
}
269-
270269
if (Array.isArray(item[field])) {
271270
item[field].forEach((v) => {
272271
if (!item[field]) {

tests/searchSpec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ describe('search', function () {
6565
done();
6666
});
6767

68+
it('searches no items with filters and query', function test(done) {
69+
const itemsjs = itemsJS([], configuration);
70+
71+
const result = itemsjs.search({
72+
filters: {
73+
tags: ['a'],
74+
category: ['drama'],
75+
},
76+
query: 'comedy',
77+
});
78+
79+
assert.equal(result.data.items.length, 0);
80+
assert.equal(result.data.aggregations.in_cinema.buckets.length, 0);
81+
assert.equal(result.data.aggregations.category.buckets.length, 0);
82+
assert.equal(result.data.aggregations.year.buckets.length, 0);
83+
84+
done();
85+
});
86+
6887
it('searches with two filters', function test(done) {
6988
const itemsjs = itemsJS(items, configuration);
7089

@@ -244,7 +263,7 @@ describe('search', function () {
244263
} catch (err) {
245264
assert.equal(
246265
err.message,
247-
'"query" and "filter" options are not working once native search is disabled',
266+
'"query" and "filter" options are not working once native search is disabled'
248267
);
249268
}
250269

0 commit comments

Comments
 (0)