Belphemur/node-json-db

Improve the docs to say what happens when getIndex fails to find

Closed this issue · 0 comments

This should be pretty obvious, and mirrors native JS behavior of find, but I was stuck for a while when a find returned the last index in my array.

When you do a find and it misses, it returns -1. Indexing the array with -1 returns the last object. Nothing is broken here, just suggesting you add a small note saying that's what will happen and you need to check the index before using it. I'm happy to make a PR as well.

const employeeIndex = Client.getIndex(`/employees`, id, 'id') // returned -1

const employee = Client.getObject<Employee>(`/employees[${employeeIndex}]`) // returned the last item in the array