Javascript 如何在模型属性中查询同一模型的对象

Javascript 如何在模型属性中查询同一模型的对象,javascript,node.js,psql,objection.js,Javascript,Node.js,Psql,Objection.js,我使用的是Objective.js,我有这个模型,希望得到与当前实例共享相同属性值的其他对象实例,例如 Example of model structure: SomeModel { property1: 'string', } Objection.js: class SomeModel extends Model{ static get tableName() { return 'some_model' } } 我想创建一个自定义属性,用于为共享相同值

我使用的是Objective.js,我有这个模型,希望得到与当前实例共享相同属性值的其他对象实例,例如

Example of model structure:

SomeModel {
  property1: 'string',
}


Objection.js: 

class SomeModel extends Model{
   static get tableName() {
       return 'some_model'
   }
}
我想创建一个自定义属性,用于为共享相同值的其他对象过滤模型,这样我就可以获得modelInstance.customProperty,它将返回一个过滤对象列表。最好的方法是什么?我尝试过使用virtualAttribute,但没有用,因为查询应该在异步函数中,而虚拟属性不支持这一点

class SomeModel extends Model{
   static get tableName() {
       return 'some_model'
   }

   static get virtualAttributes() {
       return ['customProperty'];
   }

   async customProperty() {
      return SomeModel.query().where('property1', this.property1)
   }
}
我知道这种方法是错误的,但我希望你了解我在寻找什么

编辑: 所以我试着用这种方法代替,但我不确定这是否是最好的方法

class SomeModelHelper extends Model {
    static get tableName() {
        return 'some_model';
    }
}

class SomeModel extends Model{
   static get tableName() {
       return 'some_model';
   }

   static get virtualAttributes() {
       return ['customProperty'];
   }

   async $afterFind(args) {
       await SomeModelHelper.query()
       .where('property1', this.property1)
       .then(results => this.customProperty = results);
   }
}
多亏了@rashomon的评论,我成功地解决了这个问题

class SomeModel extends Model{
   static get tableName() {
       return 'some_model';
   }

   $afterFind(args) {
       SomeModel.query()
       .where('property1', this.property1)
       .then(results => this.customProperty = results);
   }
}

您可以尝试使用
afterFind
hook:

class SomeModel扩展了模型{
静态get tableName(){
返回'some_model'
}
异步$afterFind(args){
this.customProperty=等待this.query()
.where('property1',this.property1)
}
}

不起作用,customProperty未定义,是否需要在virtualAttribute中定义它?我试着创建一个类似于SomeModelHelper的助手模型,它与SomeModel相同,并在查询中使用它,但这是最好的方法吗?我注意到使用async$afterFind代替静态async$afterFind是可行的,结合我提到的助手模型,
$afterFind
是有意义的。但是创建辅助对象模型应该不是必需的。它在引用某个模型时不起作用?(更新了答复)。另外,/catch语法不应该与async/await.Nope混合使用,我假设它只是进入了一个无限循环,所以它没有尝试指出它,只使用SomeModel.query().then()而不使用async/await,有趣的是,你的答案进入了一个无限的循环。你会来的:)如果你还想在不使用助手的情况下尝试一下,我想你可以使用
this.query().where('property1',this.property1)
(或者
this.query().where('property1',this.property1)
,我不熟悉v2新方法)