How to loop over rows after .fetchAll Bookshelf js + knex js?

bookshelf withrelated
bookshelf-modelbase
bookshelf js attach
bookshelf js pagination
bookshelf npm
bookshelf hasmany through
knex tutorial
bookshelf collection forge

I have a MySQL Database which I need to query from node.js

I am using bookshelf and knex for this.

I want to get the contents of a table - I have defined a table in my model.js file. I am attempting the query like this:

//select * from completedSentences;
Model.CompletedSentences.fetchAll().then(function (resData) {
        console.log(resData)
    })

I would like to know how to loop over resData because it should be multiple rows.

The output of the console looks like this: I dont see a list of rows I can loop over.. What am i missing?

CollectionBase {
  model:
   { [Function]
     NotFoundError: [Function: ErrorCtor],
     NoRowsUpdatedError: [Function: ErrorCtor],
     NoRowsDeletedError: [Function: ErrorCtor] },
  length: 1,
  models:
   [ ModelBase {
       attributes: [Object],
       _previousAttributes: [Object],
       changed: {},
       relations: {},
       cid: 'c4',
       id: 1 } ],
  _byId:
   { '1':
      ModelBase {
        attributes: [Object],
        _previousAttributes: [Object],
        changed: {},
        relations: {},
        cid: 'c4',
        id: 1 },
     c4:
      ModelBase {
        attributes: [Object],
        _previousAttributes: [Object],
        changed: {},
        relations: {},
        cid: 'c4',
        id: 1 } },
  _knex: null,
  _events: {},
  _eventsCount: 0 }

I found the answer (the documentation is very cryptic, hope this helps others)

new Model.CompletedSentences().fetchAll().then(function (resData) {
        _.each(resData.models, function (model) { //I am looping over models using underscore, you can use any loop
            console.log(model.attributes)
        })

    })

How to loop over rows after .fetchAll Bookshelf js + knex js?, I have a MySQL Database which I need to query from node.js I am using bookshelf and knex for this. I want to get the contents of a table - I have defined a table  javascript. Score 22. Posts 13. 21 How to loop over rows after .fetchAll Bookshelf js + knex js? Nov 24 '15. 18 How to loop over rows after .fetchAll Bookshelf js

Model.CompletedSentences.fetchAll().then(function (resData) {
        console.log(resData.serialize())
    })

output is in json format

http://bookshelfjs.org/#Model-instance-serialize

Bookshelf.js, Come join us in the #bookshelf freenode IRC channel for support on knex.js and to exit after your script has finished, you will have to call .destroy(cb) on the knex Make collections iterable using for of loops: #1830; Add row-level locking Fix inability to attach belongsToMany relation to models fetched with fetchAll()  Bookshelf.js/Knex.js innerJoin withRelated. GitHub Gist: instantly share code, notes, and snippets.

The Collection class has a set of lodash methods for this.

You can use collection.forEach this way:

new Model.CompletedSentences().fetchAll().then(function (completedSentences) {
        completedSentences.forEach(function (model) {
            console.log(model.attributes)
        })    
    })

Check out the docs, there are many other useful methods for Collection.

API Reference, A reference to the Knex.js instance being used by Bookshelf. Converts all string values to lower case when setting attributes on a model When fetching inside a transaction it's possible to specify a row-level lock by passing When defining a model you should use the bookshelf.model method, since it Model#​fetchAll  Bookshelf js fetchAll withRelated option returns empty relation object - attempt to reproduce - package.json

If you dont want to use lodash, you can do this:

new Model.CompletedSentences().fetchAll().then(function (resData) {
    resData.models.forEach( function (model) { 
        console.log(model.get('attribute');
        console.log(model.related('sth').get('attribute');
    })

})

User A.D, How to loop over rows after .fetchAll Bookshelf js + knex js? javascript bookshelf.​js knex.js asked Nov 24 '15 at 23:48. stackoverflow.com · 13 votes  Getting Started with Bookshelf.js In the last three years we have seen a spike in JavaScript’s popularity. Over the years, there have been multiple attempts at taking the popular language to the

simply just console with it attributes.

console.log(resData.attributes);

you will get results Objects vise.

User pietrovismara, 6 How to loop over rows after .fetchAll Bookshelf js + knex js? 6 Nodejs https request UNABLE_TO_GET_ISSUER_CERT_LOCALLY · View more network posts  specify columns to load with relations #75. here are my models that are being called in my fetchAll() I'm using bookshelf.js version 0.8.1 and knex version 0.8.6.

Can you provide a pagination example? · Issue #435 · bookshelf , In the knex docs they refer to count but it genereates a SQL statement.. so.. yeah, i'm lost, I want Question: fetchAll (paginated) and count total with relation query #714 Best practice for iterating over every row of a table? We also started an open discussion about the future of Bookshelf.js here #1600. I recently started a project where I was tasked to use Bookshelf.js to interact with the database. The library its self is pretty handy, it is built on top of Knex.js, a great query builder that works with Postgres, MySQL, and SQLite, but my major gripe was an overall lack of examples in the documentation.

Bookshelf js fetchAll withRelated option returns empty relation object , Bookshelf js fetchAll withRelated option returns empty relation object - node.js. which works when copy/pasted into the sqlite3 console (after putting in valid IDs of notNullable(); table.timestamps(); }).then(function(){ return knex.schema. The object is suppoosed to iterate over all objects it references before saving. The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year. If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it.

Browse codedumps, Javascript parseFloat(number) - parseFloat(number2) returning int How to loop over rows after .fetchAll Bookshelf js + knex js? The issue here is that Knex, the database abstraction layer used by Bookshelf, uses connection pooling and thus keeps the database connection open. If you want your process to exit after your script has finished, you will have to call .destroy(cb) on the knex property of your Bookshelf instance or on the Knex instance passed during initialization.

Comments
  • Wow I looked all over the documentation and could not find how to do this simple thing
  • As for me I found that result.lenght is not equal result.models.length (((
  • This should be the accepted answer. Simple and direct.
  • You the real MVP @RalleSaid
  • Note that using lodash directly on Collection objects doesn't work (i.e., don't treat them like arrays and say _.forEach(collection, cb). You have to call the methods on the collection: collection.forEach(cb), as bookshelf turns that into _.forEach(collection.models, cb) under the covers for you).