2016-08-15 24 views
0

我從Loopback中的Postgres SQL查詢獲取JSON中的模型數據數組。使用現有數據填充Loopback模型

我希望能夠直接使用此數據填充環回模型 - 這大概是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 {} 
}) 

有誰知道如何從這裏返回環回模型?

回答

0

原來答案是很容易

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) 
}) 

希望幫助別人

相關問題