inquisitive
inquisitive

Reputation: 3974

ember js association access backend nodejs mongoose

I am a noob in Ember js/node js/mongo db, and find hard to implement a simple case. I am using nodejs with mongoose as the backend, and emberjs for front end. The requirement is:

A user can belong to many organizations and an organization can have many users. A user can be admin or a customer. This is how I have modelled this in the backend:

models/organization.js

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema;

var OrganizationSchema = new Schema({
  org_name: String,
  org_address: String,
  org_admins: [{ type: Schema.Types.ObjectId, ref: 'User' }],
  org_customers: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});

module.exports = mongoose.model('Organization',OrganizationSchema);

models/user.js

var mongoose = require('mongoose');  
var Schema = mongoose.Schema;

var UserSchema = new Schema({
   username: {type: String, unique: true},
   password: String,
   role: String,
   //organizations: [{type: Schema.Types.ObjectId, ref: 'Organization'}]
});

module.exports = mongoose.model('User',UserSchema);

then I have CRUD for Users. I created some organizations storing some users ids as org_admins, and some as org_customers.

I want to create an index view, where all the organizations come up, with corresponding admins and corresponding customers.

This is how the basic template appears on the browser now:

Organizations:
Org1  address1
admins:

5534a5172751c5b05b7e7cd0
5534cb359262868030211796
Customers:


org2  address2
admins:

5534a5172751c5b05b7e7cd0
customers:
5534cb359262868030211796

I want the username of the admins and username of the customers to be displayed.

This is the Ember side code:

app/models/organization.js

import DS from 'ember-data';

export default DS.Model.extend({
  org_name: DS.attr('string'),
  org_address: DS.attr('string'),
  org_admins: DS.attr(),
  org_customer: DS.attr()

});

app/models/user.js

import DS from 'ember-data';

var User =  DS.Model.extend({
username: DS.attr('string'),
password: DS.attr('string'),
role: DS.attr('string')
});

export default User;

This is the template file app/templates/organization.hbs

<ul>
Organizations: 
<p>
{{#each organization in model}}
<li>
<label> {{organization.org_name}} </label>
<label> {{organization.org_address}}</label>
<p> <label> admins: </label></p>
 <ul>
  {{#each admin in organization.org_admins}} 
   <li>{{admin}}</li>
  {{/each}}
</ul>


<p> <label> Customers: </label></p>
 <ul>
  {{#each customer in organization.org_customers}} 
   <li>{{customer}}</li>
  {{/each}}
</ul>
</li>
{{/each}}
</p>
</ul>


<p>
<button {{action 'neworganization'}} > Add Organization </button>
</p>

{{outlet}}

The routes file app/routes/organizations.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(){
    return this.store.find('organization');
}
});

I proceeded by getting the username in the controller, but it didnt work. I understand I am doing something wrong in modelling the associations. I also tried using DS.hasMany instead of DS.attr(), but could not achieve my motive, I may be doing something wrong, not understanding the associations properly.

What is the right way I could do this. Please help.

Upvotes: 0

Views: 178

Answers (1)

Jakeii
Jakeii

Reputation: 1273

Take a look at the section on defining releationships in the guide http://guides.emberjs.com/v1.11.0/models/defining-models/

When you use DS.hasMany you need a corresponding DS.belongsTo on the other model.

Another thing to watch out for would be making sure you either include all the corresponding user models in your organisation response payload, or mark the relationship with {async: true} so ember knows to retrieve those records separately if it needs them.

Upvotes: 1

Related Questions