使用现有数据填充环回模型
Populating Loopback models with existing data
我在 JSON 中从 Loopback 中的 Postgres SQL 查询中获得了一组模型数据。
我希望能够直接用这些数据填充环回模型——这大概是 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 {}
})
有人知道如何从这里return环回模型吗?
事实证明答案相当简单
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)
})
希望对某人有所帮助
我在 JSON 中从 Loopback 中的 Postgres SQL 查询中获得了一组模型数据。
我希望能够直接用这些数据填充环回模型——这大概是 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 {}
})
有人知道如何从这里return环回模型吗?
事实证明答案相当简单
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)
})
希望对某人有所帮助