Skip to content

Instantly share code, notes, and snippets.

@Sequoia
Created May 18, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sequoia/d2562cf9ab089fd25ee453bd9dd47678 to your computer and use it in GitHub Desktop.
Save Sequoia/d2562cf9ab089fd25ee453bd9dd47678 to your computer and use it in GitHub Desktop.
hasMany relationship
/**
* @return {Promise}
* @fulfill {Array} [{HomeActivityEvent}] array of digest sent events
*/
Agent.sendDigestToClients = function(id){
let recordid;
return Agent.findById(id)
.then(function(agent){
recordid = agent.id;
return agent;
})
.then(function(agent){
return agent.clients();
})
.then(function(results){
console.log('clients!!');
console.dir(results); //undefined
console.log('id', id);
})
.then(() => Agent.app.models.User.find({'where': {'agentId': recordid}}))
.then(function(results){
results.map(user => console.log(user.agentId));
console.log('users!!');
console.dir(results.length); //1 user
});
}
...
"relations": {
"clients": {
"type": "hasMany",
"model" : "user",
"foreignKey": "agentId"
},
}
...
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"properties": {
"agentId": { "type": "string", "required": true },
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment