首页 > 解决方案 > Objection.js 中的异步 $formatDatabaseJson

问题描述

我需要在我的 $formatDatabaseJson 中等待一个函数。我正在检查文档(https://vincit.github.io/objection.js/#_s_formatdatabasejson),似乎不可能。有什么办法可以手动完成吗?

class Person extends Model {
  async $formatDatabaseJson(json) {
    // Call the super class's implementation.
    json = await super.$formatDatabaseJson(json)
    // await function here
    return json
  }
}

标签: javascriptmodelasync-awaitobjection.js

解决方案


$formatDatabaseJson是同步的,不能用于调用异步代码。$beforeInsert$beforeUpdate并且$afterGet钩子是异步的,可以根据您的用例使用。


推荐阅读