简体   繁体   English

EmberJS用户str.replace不是函数TypeError:str.replace不是函数

[英]EmberJS users str.replace is not a function TypeError: str.replace is not a function

So I'm just playing around with EmberJS and I really want to learn EmberJS, but I've gotten stuck on this issue. 所以我只是在玩EmberJS,我真的很想学习EmberJS,但是我一直陷在这个问题上。 I'm doing a api call and I think I've formatted the json in the way the EmberJS RESTAdapter requires but I'm not totally sure. 我正在进行api调用,我想我已经按照EmberJS RESTAdapter要求的方式格式化了json,但我不确定。

My router: 我的路由器:

App.Router.map( function() {

    this.route('about', { path: '/about' });
    this.route('users', { path: '/users' });
});

My Users route: 我的用户路线:

App.UsersRoute = Ember.Route.extend({
    model: function() {

        /*
        this.store.findAll('user').then( function( results ) {
            console.log( results );
        }, function() {
            console.log('failed');
        });
        */

        return this.store.findAll('user');
    }
});

My app.js 我的app.js

var App = Ember.Application.create();

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://api.diverseevolution.com'
});


App.ApplicationSerializer = DS.RESTSerializer.extend({
    primaryKey: 'id',
    serializeId: function( id ) {
        return id.toString();
    }
});

My template: 我的模板:

<script type="text/x-handlebars" data-template-name="users">
    <p>This is the users route, the goal is to plugin the users fixtures.</p>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>First name:</th>
                <th>Last name:</th>
            </tr>
        </thead>
        <tbody>
            {{#each model as |user|}}
            <tr>
                <td>{{ user.id }}</td>
                <td>{{ user.first_name }}</td>
                <td>{{ user.last_name }}</td>
            </tr>
            {{else}}
            <tr>
                <td colspan="3">No users found...</td>
            </tr>
            {{/each}}
        </tbody>
    </table>
</script>

My model: 我的模特:

App.User = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    email: DS.attr('string')
});

And finally my json response from the service: 最后是服务的我的json响应:

{  
    "users":[  
        {  
            "id":"1",
            "firstName":"Thomas",
            "lastName":"Bird",
            "address":"4035 Stonebridge",
            "city":"Holly",
            "state":"MI",
            "zip":"48442",
            "email":"godoploid@gmail.com",
            "status":{  
                "id":"1",
                "title":"active",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            },
            "userTypeId":"1",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null,
            "type":{  
                "id":"1",
                "title":"Admin",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            }
        },
        {  
            "id":"2",
            "firstName":"Avery",
            "lastName":"Crain",
            "address":"282 Mansfield Dr.",
            "city":"Lapeer",
            "state":"MI",
            "zip":"48446",
            "email":"avery.crain@gmail.com",
            "status":{  
                "id":"1",
                "title":"active",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            },
            "userTypeId":"2",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null,
            "type":{  
                "id":"2",
                "title":"User",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            }
        },
        {  
            "id":"3",
            "firstName":"Mike",
            "lastName":"Marshall",
            "address":"4890 Lawndale St.",
            "city":"North Branch",
            "state":"MI",
            "zip":"48461",
            "email":"mjmarshall@gmail.com",
            "status":{  
                "id":"1",
                "title":"active",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            },
            "userTypeId":"2",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null,
            "type":{  
                "id":"2",
                "title":"User",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            }
        },
        {  
            "id":"4",
            "firstName":"Kylar",
            "lastName":"Bird",
            "address":"5875 Gracelawn",
            "city":"North Branch",
            "state":"MI",
            "zip":"48461",
            "email":"kbird@gmail.com",
            "status":{  
                "id":"1",
                "title":"active",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            },
            "userTypeId":"2",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null,
            "type":{  
                "id":"2",
                "title":"User",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            }
        }
    ]
}

The live api can be found here: http://api.diverseevolution.com/users 实时api可以在这里找到: http : //api.diverseevolution.com/users

This is the error I'm getting: 这是我得到的错误:

在此处输入图片说明

I for the life of me can not figure out why I keep getting this error, I've changed the api to a number of formats described in the Ember docs, I've also changed the serializer, adapter and still can't figure out why I'm getting this error, if anyone could help I would greatly appreciate it. 我一生都无法弄清楚为什么仍然会收到此错误,我将api更改为Ember文档中描述的多种格式,我也更改了序列化器,适配器,但仍然无法弄清楚为什么我遇到此错误,如果有人可以提供帮助,我将非常感激。 I'm really trying to wrap my head around EmberJS but I've been stuck here on this for the last two days! 我真的想尽全力围绕EmberJS,但是最近两天我一直在这里停留!

Thanks 谢谢

Looks like I can't reproduce your error and it seems to me if you: 看来我无法重现您的错误,并且在您看来:

  • fix typos in template firstName instead of first_name 修复模板firstName而不是first_name拼写错误
  • remove ApplicationSerializer (there is nothing different from default) 删除ApplicationSerializer (与默认设置没有什么不同)

your code will work. 您的代码将起作用。

Working jsbin on ember 2.1 在ember 2.1上工作jsbin

It appears that EmberJS didn't like the multiple relationships I was sending back from the api. 看来EmberJS不喜欢我从api发回的多个关系。

{  
            "id":"3",
            "firstName":"Mike",
            "lastName":"Marshall",
            "address":"4890 Lawndale St.",
            "city":"North Branch",
            "state":"MI",
            "zip":"48461",
            "email":"mjmarshall@gmail.com",
            "status":{  
                "id":"1",
                "title":"active",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            },
            "userTypeId":"2",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null,
            "type":{  
                "id":"2",
                "title":"User",
                "created_at":"-0001-11-30 00:00:00",
                "updated_at":"-0001-11-30 00:00:00",
                "deleted_at":null
            }
        }

Specifically the status and type once I got rid of those everything work. 一旦我摆脱了所有问题,便会具体说明statustype The response I have now looks like this and works perfectly: 我现在的响应看起来像这样,效果很好:

{  
            "id":"1",
            "firstName":"Thomas",
            "lastName":"Bird",
            "address":"123 Somewhere Rd",
            "city":"Somewhere",
            "state":"MI",
            "zip":"98675",
            "email":"godoploid@gmail.com",
            "status":"1",
            "userTypeId":"1",
            "createdAt":"-0001-11-30 00:00:00",
            "updatedAt":"-0001-11-30 00:00:00",
            "deletedAt":null
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM