javascript - Why do we need a new object in bookshelf.js fetch() method? -


i'm new node , using bookshelf.js orm current project. documentation of bookshelf.js contains snippet under fetch() method:

// select * `books` `isbn-13` = '9780440180296' new book({'isbn-13': '9780440180296'})   .fetch()   .then(function(model) {   // outputs 'slaughterhouse five'   console.log(model.get('title')); }); 

http://bookshelfjs.org/#model-fetch

what confuses me why need create new object here if querying existing record? way bookshelf.js works requires new object created every returned result?

this confusing. @ moment there 1 type of object represents both "model" , "query builder".

fetch prefills where clause set attributes on model.

there plans underway change this. discussion here.

instead of writing code above, i'd recommend doing this:

// select * `books` `isbn-13` = '9780440180296' book.where({'isbn-13': '9780440180296'})   .fetch()   .then(function(model) {   // outputs 'slaughterhouse five'   console.log(model.get('title')); }); 

(i realize you've copied straight docs.)

if wish update model in place, prefer use new model#refresh:

book.forge({id: 5}).refresh().then(function (book) {//... 

Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -

java - How can I send the data from a imput type="file" to the controller? -