vintorg
vintorg

Reputation: 217

How to get JSON collections using Bookshelf

I am using Bookshelf, and I want to do something like this:

function myfunc() {
    this.vals = Accounts.forge().fetch();
}

I can get the collection object, but I can't call .toJSON on it to store in this.vals. What am I missing? I can access the collection in a .then() promise:

.then(function(collection) {
    console.log(collection.toJSON());
})

How can I store this JSON in this.vals?

Upvotes: 0

Views: 3763

Answers (1)

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

If I understand you correctly, the fetch() call returns a promise that is fulfilled via then(). The promise object itself has no toJSON() method. You'd need something like this:

function myfunc() {
    this.vals = Accounts.forge().fetch()
        .then(function(accounts) {
            return accounts.toJSON();
        });
}

Upvotes: 4

Related Questions