空的内容来自承诺? [英] Empty content from promise?

查看:134
本文介绍了空的内容来自承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ember-data ,我使用

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

我的代码进入// 1,问题是我得到这个似乎无效的对象。

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

这是我的api发回的。

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"
    }
  ]
}

这是我的模型声明

//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')
});

谢谢!

推荐答案

根据您使用的Ember版本,假设您的环境模型设置良好(也许您可以列出这些,以便我们可以排除),您会希望服务器以两种格式之一发送回数据,以使Ember Data快乐:

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(或更早版本),其默认值为$ code 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
    }
  ]
}


  • Ember 2.0+及其默认 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
          }
        }
      ]
    }
    


  • 您的模型可以删除ID属性,就像Ember假定并为您添加的一样。

    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.

    这篇关于空的内容来自承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆