我正在使用postgresql,knex和書架進行查詢以更新我的用戶表。我想查找所有在特定時間內未登錄的用戶,然後更新他們的numAbsences和numTardies字段。如何返回並更新書架中的表knex
但是,使用bookshelf.knex運行原始sql查詢時,我得到的結果是用戶的對象數組而不是對象的書架對象數組,因爲我無法直接將對象保存到數據庫,當我嘗試使用.save()。我得到例外user.save is not a function
。
有誰知道我可以如何更新數據庫中的值爲用戶?我已經看到了更新功能,但我還需要在absentUsers中返回用戶,因此我現在選擇它們。
// field indicates whether the student was late or absent
var absentUsers = function(field){
// returns all users who did not sign in during a specific time
if (ongoingClasses){
return bookshelf.knex('users')
.join('signed_in', 'signed_in.studentId', '=', 'users.id')
.where('signed_in.signedIn', false)
.select()
.then(function(users){
markAbsent(users, field);
return users;
});
}
}
var markAbsent = function(users, field){
users.forEach(function(user){
user[field]++;
user.save();
})
}