22

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 }
A.D
  • 1,480
  • 2
  • 18
  • 33

6 Answers6

21

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)
        })

    })
A.D
  • 1,480
  • 2
  • 18
  • 33
16
Model.CompletedSentences.fetchAll().then(function (resData) {
        console.log(resData.serialize())
    })

output is in json format

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

RalleSaid
  • 381
  • 3
  • 12
7

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.

pietrovismara
  • 6,102
  • 5
  • 33
  • 45
  • 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). – kris Mar 23 '18 at 15:14
3

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');
    })

})
Salar
  • 5,305
  • 7
  • 50
  • 78
0

simply just console with it attributes.

console.log(resData.attributes);

you will get results Objects vise.

Apurv Chaudhary
  • 1,672
  • 3
  • 30
  • 55
-3

Answer is simple, try this console.log(resData.toJSON())

akshara
  • 1
  • 4