我想从排名仅为数字的用户模型计算排名。 排名示例:
,并且当用户等级为150点时,它应为银色等级。我可以从虚拟列计算等级以查询等级表来获得等级名称吗?
"use strict";
module.exports = (sequelize, DataTypes) => {
const Rank = sequelize.define("Rank", {
name: DataTypes.STRING,
tier_total: DataTypes.INTEGER
});
const UserGameID = sequelize.define(
"UserGameID",
{
user_id: DataTypes.INTEGER,
total_win: {
type: DataTypes.INTEGER,
defaultValue: 0
},
total_lose: {
type: DataTypes.INTEGER,
defaultValue: 0
},
total_match: {
type: DataTypes.INTEGER,
defaultValue: 0
},
total_point: {
type: DataTypes.INTEGER,
defaultValue: 0
},
rank: {
type: DataTypes.VIRTUAL,
get: async function() {
var rank = await Rank.findOne({
where: {
tier_total: { $lte: this.total_point }
},
order: [["tier_total", "desc"]]
});
return rank;
}
},
status: DataTypes.BOOLEAN //false: unapproved, true: approved
}, { }
);
预期结果:
user_id: 1
total_win: 2
total_lose: 1
total_match: 3
total_point: 150
rank: silver
但排名总是返回{}