mardi 13 décembre 2016

Ember+Mongoose doesn't return records

Well I'm having an issue with ember and the models, because I want to return a JSON from a Mongoose API, using Express, the issue it's that the route return the json correctly, but when I try to catch it in the front end (Ember) it's returning like null.

In my Ember route I had the following:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params){ //i'm getting params from the url as a paremeter
    return Ember.RSVP.hash({

      questions: this.store.find('examGroupQuestion/show-q', params.questionsGroup_id)
    });
  }
});

It's important that this route it's child from another one, next I show you the router:

import Ember from 'ember';
import config from './config/environment';

const Router = Ember.Router.extend({
  location: config.locationType,
  rootURL: config.rootURL
});

Router.map(function() {
 this.route('examGroupQuestion',function() {
   this.route('showQ',{
     path: 'showQ/:questionsGroup_id'
   });
 });
});
export default Router;

And finally this is my model:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({ 
/* the atributte array it's defined in a transform */
  description: attr('string'),
  matrix_eval: attr('array'),
  level: attr('string'),
  rows: attr('array'),
  randomize_row: attr('boolean'),
  columns: attr('array'),
  randomize_col: attr('boolean'),
});

Well having this, we are trying to pass the json from the route model to the template, but the record always show that it's null, like in the next image that we can see the REST Server response and the error:

This is the error that Ember returns

In the REST Server I had this route:

router.route('/examGroupQuestion/showQs/:qid').get(function(req,res){ questions_group.getQuestions(req,res, req.params.qid); });

and in the api I had the following:

module.exports.getQuestions = function(req,res,id){
  QuestionGroup.findById(id).exec(function(err, questiongroup){
    if(err)
    {
      res.send(err);
    }

    function done(result){
      res.json({questionsGroup:result});
    }

    var result = [];
    var l = questiongroup.questions.length;
    for(var j = 0; j < l; j++)
    {
      (function(j){
        Questions.findById(questiongroup.questions[j].id).exec(function(err, questions){
          if(err)
          {
            res.send(err);
          }
          result.push(questions);
          if(l === j+1)
          {
            done(result);
          }
        });
      })(j);
    }
  });
};

Does anyone know why it's this happening, any the help it's appreciated.




Aucun commentaire:

Enregistrer un commentaire