hbi99 / defiant.js

http://defiantjs.com
GNU Affero General Public License v3.0
913 stars 91 forks source link

Solve search with parameter single=true and no results #119

Open estercolero82 opened 5 years ago

estercolero82 commented 5 years ago

When doing a defiant.search with the option single=true and no results found throws an exception: xres[i] is null. This commit solves the problem. Sample code: (async () => { // import 'defiant' var defiant = await fetchScript('/res/js/modules/defiant.js'); var data = { "car": [ {"id": 10, "color": "silver", "name": "Volvo"}, {"id": 11, "color": "red", "name": "Saab"}, {"id": 12, "color": "red", "name": "Peugeot"}, {"id": 13, "color": "yellow", "name": "Porsche"} ], "bike": [ {"id": 20, "color": "black", "name": "Cannondale"}, {"id": 21, "color": "red", "name": "Shimano"} ] };

//Search with 0 results looking for a single node var search = defiant.search(data, '//car[color="brown"]/name', true); console.log(search); })();