【发布时间】:2020-02-19 13:51:27
【问题描述】:
我向模型添加了一个名为 customMethod 的自定义方法,如下所示:
class Prize extends Model {
customMethod(){
return 'test'
}
}
当我使用find通过主键获得奖品时,我可以调用这个方法没有问题
const prize = await Prize.find(1);
return prize.customMethod();
//returns 'test'
但是,如果我以任何其他方式获得奖品,通过关系或通过字段查询,我无法访问此方法。
const countrysPrizes = await country.prizes().fetch();
for (const key in countrysPrizes) {
if (countrysPrizes.hasOwnProperty(key)) {
const prize = countrysPrizes[key];
return prize.customMethod();
//returns 500 - prize.customMethod is not a function
}
}
如何在遍历多个对象时访问此方法?
【问题讨论】:
标签: adonis.js