Rbutler93
Rbutler93

Reputation: 73

Not retrieving any information from model using Ember.js using query() method

I'm trying to learn Ember.js by building a web application using the framework for the front end and node.js for the backend as well as mongoDB as a database.

I am using/attempting to use the url to feed in a parameter id and then query on that id.

I am having trouble grabbing the information from my model. My query works fine when I am using findAll(). However, when I switch to find() or query() I can't retrieve any results.

All the documentation I have looked at use different syntax that I have tried but none have worked.

I would greatly appreciate any hint or input. Thank you.

app/router.js

    Router.map(function() {
      this.route('stock', { path: '/:stock_id' });
    });

app/routes/stock.js

    export default Ember.Route.extend({
       model: function(params) {
         this.store.query('stock', {filter:{id : params.stock_id}}).then(function(stock){return stock.get('firstObject')});
       }
    });

app/models/stock.js

    export default DS.Model.extend({
       Symbol: DS.attr('string'),
       Name: DS.attr('string'),
       LastSale: DS.attr('string'),
       MarketCap: DS.attr('string'),
       IPOyear: DS.attr('string'),
       Industry: DS.attr('string')
    });

app/serializers/stock.js

    export default DS.RESTSerializer.extend({
       primaryKey: '_id',
       serializeId: function(id) {
          return id.toString();
       }
    });

app/templates/stock.hbs

   {{#each model as |item|}}
   <h3>{{item.Name}}</h3>
   {{/each}}

my nodejs server

     app.get('/api/stocks/:id', function(req, res){
        StockModel.findOne({'_id': req.params.id},function(err,docs){
           console.log(docs.symbol);
           if(err){
              res.send({error:err});
           }
           else{
             res.send({stock:docs});
           }
       });
    });

Upvotes: 0

Views: 128

Answers (2)

alizahid
alizahid

Reputation: 969

You need to return the query from the model. Update your app/routes/stock.js to look like

export default Ember.Route.extend({
  model(params) {
    return this.store
      .query('stock', {
        filter: {
          id: params.stock_id
        }
      })
      .then(stock => stock.get('firstObject'))
  }
})

Upvotes: 0

Ember Freak
Ember Freak

Reputation: 12872

Model hook should return the result, only then this will be set in model in corresponding controller through setupController method hook.

model: function(params) {
         return this.store.query('stock', {filter:{id : params.stock_id}}).then(function(stock){return stock.get('firstObject')});
       }

Update: Your model field name should be camelCased

// app/models/stock.js
export default DS.Model.extend({
symbol: DS.attr('string'),
name: DS.attr('string'),
lastSale: DS.attr('string'),
marketCap: DS.attr('string'),
iPOyear: DS.attr('string'),
industry: DS.attr('string')
});

and ensure, your backend is accepting the filter and returning the required result in the below format,

"stocks": [
{
    "id": 1,
    "symbol": "foo",
    "name": "foo",
    "lastSale": "foo",
    "marketCap": "foo",
    "iPOyear": "foo",
    "industry": "foo" 
}]

Upvotes: 1

Related Questions