如何使用 Bookshelf 获取 JSON collections
How to get JSON collections using Bookshelf
我正在使用 Bookshelf,我想做这样的事情:
function myfunc() {
this.vals = Accounts.forge().fetch();
}
我可以得到 collection object,但我不能调用它的 .toJSON 来存储在 this.vals 中。我错过了什么?我可以在 .then() promise:
中访问 collection
.then(function(collection) {
console.log(collection.toJSON());
})
如何将此 JSON 存储在 this.vals 中?
如果我没理解错的话,fetch()
调用 returns 一个通过 then()
实现的承诺。 promise 对象本身没有 toJSON()
方法。你需要这样的东西:
function myfunc() {
this.vals = Accounts.forge().fetch()
.then(function(accounts) {
return accounts.toJSON();
});
}
我正在使用 Bookshelf,我想做这样的事情:
function myfunc() {
this.vals = Accounts.forge().fetch();
}
我可以得到 collection object,但我不能调用它的 .toJSON 来存储在 this.vals 中。我错过了什么?我可以在 .then() promise:
中访问 collection.then(function(collection) {
console.log(collection.toJSON());
})
如何将此 JSON 存储在 this.vals 中?
如果我没理解错的话,fetch()
调用 returns 一个通过 then()
实现的承诺。 promise 对象本身没有 toJSON()
方法。你需要这样的东西:
function myfunc() {
this.vals = Accounts.forge().fetch()
.then(function(accounts) {
return accounts.toJSON();
});
}