gpt4 book ai didi

javascript - Bookshelf JS 访问模型的 idAttribute 属性的值

转载 作者:行者123 更新时间:2023-12-01 03:39:50 26 4
gpt4 key购买 nike

我在访问模型中的 ID 时遇到问题。我知道模型可以有一个我定义的属性 idAttribute 。我的问题是我想使用模型的 id 进行查询,但我不知道如何操作。我已阅读 bookshelfJS 文档,但无济于事。任何帮助是极大的赞赏。我的代码看起来像这样:

let Recipe = bookshelf.Model.extend({
tableName: 'recipes',
idAttribute: 'id',
visible: ['name', 'description', 'prep_time', 'cook_time', 'notes',
'image_url', 'ingredients', 'instructions', 'tags', 'votes'],
hasTimestamps: true,


votes: function() {
// return this.hasMany(RecipeVote);
bookshelf.knex('recipes_votes')
.sum('vote')
.where('recipe_id', `I WANT MY MODELS ID HERE`)
.groupBy('recipe_id')
.then(function (result) {
// cb(null, sum)
console.log('SUM:', result[0].sum);
return result[0].sum;
})
.catch(function (err) {
// cb(err)
console.log('THERE WAS AN ERROR:', err);
});
}
});

最佳答案

我认为你可以通过以下方式访问 id 属性:


this.attributes.id

或者


this.get('id')

关于javascript - Bookshelf JS 访问模型的 idAttribute 属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com