Empty content from promise?

。_饼干妹妹 提交于 2019-12-11 12:16:35

问题


I'm using ember-data, I call to my API using

this.store.findAll('environment').then(function(values){
  //1
},function(reason){
  //rejected
});

And my code does go into the //1, problem is I get this object which seems pretty invalid.


Here is what my api sends back.

{
  "data": {
    "environments": [
      {
        "id": 1,
        "localePath": "C:\\XML_DEPOT",
        "name": "Acceptation 1",
        "remotePath": "D:\\XML_DEPOT",
        "databaseServerName": "blabla",
        "databaseName": "blabla",
        "port": 60903
      },
      {
        "id": 2,
        "localePath": "bob",
        "name": "Acceptation 2",
        "remotePath": "bob",
        "databaseServerName": "blabla\\blabla",
        "databaseName": "blabla",
        "port": 60904
      }
    ]
  }
}

on the second try i gave it this and still didnt like it.

{
  "data": [
    {
      "id": 1,
      "localePath": "C:\\XML_DEPOT",
      "name": "Acceptation 1",
      "remotePath": "D:\\XML_DEPOT",
      "databaseServerName": "W050A01SQL1",
      "databaseName": "MAMROT01P1_MSCRM",
      "port": 60903,
      "type": "environments"
    },
    {
      "id": 2,
      "localePath": "bob",
      "name": "Acceptation 2",
      "remotePath": "bob",
      "databaseServerName": "W050A01SQL1\\W050A01SQL1B02",
      "databaseName": "MAMROT01P1_MSCRM",
      "port": 60904,
      "type": "environments"
    }
  ]
}

this is my model declaration

//environment.js
export default Model.extend({
  id: DS.attr('number'),
  localePath: DS.attr('string'),
  name: DS.attr('string'),
  remotePath: DS.attr('string'),
  databaseServerName: DS.attr('string'),
  databaseName: DS.attr('string'),
  port: DS.attr('number')
});

Thank you !


回答1:


Depending on what version of Ember you are using, and assuming your environments model is set up well (maybe you could list that just so we can rule that out), you would want the server to send you back data in one of two formats to make Ember Data happy:

  1. Ember 1.13 (or earlier version) with its default DS.RESTAdapter (docs)

    {
      "environments": [
        {
          "id": 1,
          "localePath": "C:\\XML_DEPOT",
          "name": "Acceptation 1",
          "remotePath": "D:\\XML_DEPOT",
          "databaseServerName": "blabla",
          "databaseName": "blabla",
          "port": 60903
        },
        {
          "id": 2,
          "localePath": "bob",
          "name": "Acceptation 2",
          "remotePath": "bob",
          "databaseServerName": "blabla\\blabla",
          "databaseName": "blabla",
          "port": 60904
        }
      ]
    }
    
  2. Ember 2.0+ with its default DS.JSONAPIAdapter (docs)

    {
      "data": [
        {
          "type": "environments",
          "id": 1,
          "attributes": {
            "localePath": "C:\\XML_DEPOT",
            "name": "Acceptation 1",
            "remotePath": "D:\\XML_DEPOT",
            "databaseServerName": "blabla",
            "databaseName": "blabla",
            "port": 60903
          }
        },
        {
          "type": "environments",
          "id": 2,
          "attributes": {
            "localePath": "bob",
            "name": "Acceptation 2",
            "remotePath": "bob",
            "databaseServerName": "blabla\\blabla",
            "databaseName": "blabla",
            "port": 60904
          }
        }
      ]
    }
    

And your model can drop the ID attribute, as you've discovered since it's assumed and added for you by Ember.

//environment.js
export default Model.extend({
  localePath: DS.attr('string'),
  name: DS.attr('string'),
  remotePath: DS.attr('string'),
  databaseServerName: DS.attr('string'),
  databaseName: DS.attr('string'),
  port: DS.attr('number')
});

Hope that gets you what you're after.



来源:https://stackoverflow.com/questions/36772796/empty-content-from-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!