【问题标题】:Populating Loopback models with existing data使用现有数据填充 Loopback 模型
【发布时间】:2016-08-15 16:18:55
【问题描述】:

我从 Loopback 中的 Postgres SQL 查询中获得了 JSON 格式的模型数据数组。

我希望能够直接使用这些数据填充环回模型 - 大概 loopback-datasource-juggler 已经在 dao.js 组件中完成了。

很遗憾,我无法完成这项工作。到目前为止,这是我所得到的:

app.dataSources.Db.connector.execute(sql, null, (err, modelsRaw) => {
   // Fetch the data in the right casing for the model
   const preparedModels = modelsRaw.map(modelRaw => app.dataSources.Db.connector.fromRow('myModel', modelRaw))
   // Now I'm lost...
   const model = app.dataSources.Db.connector.getDataAccessObject()  // returns null
   app.dataSources.Db.connector.getDataAccessObject(preparedModels[0]) //returns {}
})

有人知道如何从这里返回 Loopback 模型吗?

【问题讨论】:

    标签: javascript postgresql loopback


    【解决方案1】:

    原来答案很简单

    app.dataSources.Db.connector.execute(sql, null, (err, modelsRaw) => {
    const models = modelsRaw.map(modelRaw => {
      const preparedModel = app.dataSources.Db.connector.fromRow('myModel', modelRaw)
      return new app.models.myModel(preparedModel)
    })
    

    希望对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2013-06-12
      • 2018-08-26
      • 2019-01-29
      • 2013-12-06
      • 2014-12-13
      • 1970-01-01
      • 2019-08-13
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多