2017-08-05 3 views
3

ループバック同形クライアントを使用して、特定の会社のプロファイルを取得したいとします。ループバック同形クライアント内の関連モデルへのアクセス

CompanyモデルはMySQLコネクタを使用し、プロファイルはElasticSearchインスタンス内に存在します。

ループバッククライアントは私のフロントエンドアプリ内で完全に働いている、次のコードが正常に実行されます。

let lbclient = window.require('lbclient') 
 

 
lbclient.models.RemoteProfile.find().done(profiles => { 
 
    // do something with the profiles array 
 
})

私の目標は、ネストされたリソースとしてプロファイルを取得することです:

/api/companies/{id}/profiles/

質問は次のとおりです。クライアントで次のコードが動作しないのはなぜですか?

// This is the code I execute on the client 
 
lbclient.models.RemoteCompany.findById(8, (err, company) => { 
 
    company.profiles // undefined 
 
}) 
 

 
// This code works very well on the server 
 
Company.findById(8, (err, company) => { 
 
    company.profiles((err, profiles) => { 
 
    // profiles belong the the company having id=8 
 
    } 
 
})

共通/モデル/ company.json

{ 
 
    "name": "Company", 
 
    "plural": "companies", 
 
    "base": "PersistedModel", 
 
    "idInjection": true, 
 
    "options": { 
 
    "validateUpsert": true 
 
    }, 
 
    "properties": { 
 
    "name": { 
 
     "type": "string", 
 
     "required": true, 
 
     "default": "My Company" 
 
    } 
 
    }, 
 
    "validations": [], 
 
    "relations": { 
 
    "profiles": { 
 
     "type": "hasMany", 
 
     "model": "Profile", 
 
     "foreignKey": "company_id" 
 
    } 
 
    }, 
 
    "acls": [], 
 
    "methods": {} 
 
}

共通/モデル/ profile.json

{ 
 
    "name": "Profile", 
 
    "plural": "profiles", 
 
    "base": "PersistedModel", 
 
    "idInjection": true, 
 
    "options": { 
 
    "validateUpsert": true 
 
    }, 
 
    "properties": { 
 
    "name": { 
 
     "type": "string" 
 
    }, 
 
    }, 
 
    "validations": [], 
 
    "relations": { 
 
    "company": { 
 
     "type": "belongsTo", 
 
     "model": "Company", 
 
     "foreignKey": "company_id" 
 
    } 
 
    }, 
 
    "acls": [], 
 
    "methods": {} 
 
}

クライアント/ループバック/モデル/リモートcompany.json

{ 
 
    "name": "RemoteCompany", 
 
    "base": "Company", 
 
    "plural": "companies", 
 
    "trackChanges": false, 
 
    "enableRemoteReplication": true 
 
}

クライアント/ループバック/モデル/リモートプロファイル。 json

{ 
 
    "name": "RemoteProfile", 
 
    "base": "Profile", 
 
    "plural": "profiles", 
 
    "trackChanges": false, 
 
    "enableRemoteReplication": true 
 
}

答えて

1

あなたは、このようなクエリでプロファイルを含める必要があります。

lbclient.models.RemoteCompany.findById(8, {include: 'profiles'}, (err, company) => { 

}) 
+0

はそれは私が思いついた最初のソリューションだった、答えてくれてありがとう、残念ながら、私はできませんそのような "カウント"を実行します。 –

+0

@AntonioTrapaniは何のために数えますか? –

+0

は、会社が所有し、フィルタの対象となるプロファイルを数えます。典型的な使用例はページネーションです。 –

関連する問題