How to access response data from node backend in ember app

My node route sends data to the front end

router.post("/key", function (req, res, next) {
  // do stuff here 
 
  res.status(201).send({
    key: "some_value_i_need_in_ember_app"
  });
})

My front-end ember

import Ember from 'ember';

export default Ember.Controller.extend({
  actions: {
    generateKey () {
      var credentials = this.getProperties("difficulty", "quantity");

      this.store.createRecord("key", credentials).save().then(record => {
        console.log("success", record);
      }, reason => {
        console.log("error", reason);
      });
    }
  }
});

However, record doesn’t give me the “key” the backend sends. How do I access that data???

I think “some_value_i_need_in_ember_app” needs to be an object, not a string

Using this.store, you use Ember Data. For Ember Data, you have to create a key model. Let say you have a content property in your key model. You can generate this model with ember-cli.

ember g model key content:string

// app/models/key.js
import DS from 'ember-data';

export default DS.Model.extend({
  content: DS.attr('string')
});

In the latest Ember.js the default adapter and format is JSON API format. http://jsonapi.org/

A very simple key model with content property json package should looks like this:

{
  "data": {
    "type": "keys",
    "id": "1",
    "attributes": {
      "message": "some_value_i_need_in_ember_app"
    },
}

When the node app persists the key record, it could respond with a JSON API object. You can attach some meta information to the response package, or you can use some other attribute, which would be part of the model.

Don’t hesitate to share more about the problem what you would like to solve and we try to figure out the best solution. :slight_smile:

I already have a model for key

app/models/key

import DS from 'ember-data';

export default DS.Model.extend({
  difficulty: DS.attr("string"),
  quantity: DS.attr("string"),
  type: DS.attr("array")
});

So do I need to add a content property in the model for the response property?

Well assuming your data looks like this:

{
    key: { /* data here */ }
}

and you have your model created, and your data has a property called id, you’ll want to use the RESTSerializer. I wrote about it on my blog not too long ago: 404 Not Found

See item 2 on that post. I think that is what you need given your sample API code.

1 Like

Really helpful article. Thank you!!!

In both cases, if you use the new JSON API adapter or REST Adapter, you can add new fields to your model. No problem if those fields populated with content on the server side. When you create a new record and persist, response the whole record. The server will generate a new ID and could have content or any custom extra field. Ember Data will update the fields based on the response. Of course, you have to add those fields to the Ember.js model class as well.

As @skaterdav85 suggested, you have to choose a format and your responses should follow those conventions.