Skip to content

Commit dcae288

Browse files
committed
Fix typicode#510 thanks to @robinvenneman
1 parent 6559219 commit dcae288

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
* Drop Node 0.12 support
6+
* Prevent `TypeError` when a filter is applied on a `null` value [#510](https://github.com/typicode/json-server/issues/510)
67

78
## [0.9.6][2016-03-08]
89

src/server/router/plural.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ module.exports = (db, name) => {
114114
const isRange = /_lte$/.test(key) || /_gte$/.test(key)
115115
const isLike = /_like$/.test(key)
116116
const path = key.replace(/(_lte|_gte|_ne|_like)$/, '')
117+
// get item value based on path
118+
// i.e post.title -> 'foo'
117119
const elementValue = _.get(element, path)
118120

119-
if (elementValue === undefined) {
121+
// Prevent toString() failing on undefined or null values
122+
if (elementValue === undefined || elementValue === null) {
120123
return
121124
}
122125

test/server/plural.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Server', () => {
2020

2121
db.posts = [
2222
{ id: 1, body: 'foo' },
23-
{ id: 2, body: 'bar' }
23+
{ id: 2, body: 'bar' },
2424
]
2525

2626
db.tags = [
@@ -163,6 +163,16 @@ describe('Server', () => {
163163
.expect(db.comments)
164164
.expect(200)
165165
))
166+
167+
// https://github.com/typicode/json-server/issues/510
168+
it.only('should not fail with null value', () => {
169+
db.posts.push({ id: 99, body: null })
170+
return request(server)
171+
.get('/posts?body=foo')
172+
.expect('Content-Type', /json/)
173+
.expect([ db.posts[0] ])
174+
.expect(200)
175+
})
166176
})
167177

168178
describe('GET /:resource?q=', () => {

0 commit comments

Comments
 (0)