简体   繁体   English

ember js中的动态细分

[英]Dynamic segments in ember js

I'm new to ember js. 我是ember js的新手。 I was trying to use dynamic segments in my ember project and it give me an error.I tried localhost/4200/profile/john in my browser to get the info of "john".I think it is complaining about api end point in server.js.. Please help me to find what i have done wrong. 我试图在余烬项目中使用动态段,这给了我一个错误。我在浏览器中尝试了localhost / 4200 / profile / john来获取“ john”的信息。我认为它正在抱怨服务器中的api端点.js ..请帮助我找出我做错了什么。

error display in console: 控制台中显示错误:

GET localhost:4500/api/users/john 404 (Not Found) GET localhost:4500 / api / users / john 404(未找到)

These are my files; 这些是我的文件;

router.js router.js

Router.map(function() {

   this.resource('profile', { path: '/profile/:username' });
});

model/user.js 型号/user.js

import DS from 'ember-data';

export default DS.Model.extend({


    docType:DS.attr('string'),
    firstName:DS.attr('string'),


    userName:DS.attr('string'),
    password:DS.attr('string'),

    lastName:DS.attr('string'),
    mobileNo:DS.attr('string'),
    landNo:DS.attr('string'),
    address:DS.attr(
        {
        no:'string',
        street:'string',
        city:'string'
    }
    ),

    nicNo:DS.attr('string'),
    created_at:DS.attr('date'),
    updated_at:DS.attr('date')

});

route/profile.js 路线/profile.js

import Ember from 'ember';

export default Ember.Route.extend({

    model: function(params, transition) {
        return this.get('store').find('user', params.username);
    }

});

server.js server.js

app.get('/api/users', function(req,res) {
    UserModel.find({},function(err,docs) {
        if(err) {
            res.send({error:err});
        }
        else {
            res.send({user:docs});
        }
    });
});

template/profile.hbs 模板/profile.hbs

<h2>Welcome user</h2>

{{#each item in model}}
{{item.userName}}
{{/each}}

You need to add an adapter to your application and tell it where your API is. 您需要向应用程序添加适配器,并告诉它您的API在哪里。

//app/adapters/application.js
import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  namespace: 'api' //All requests will be made to api/*
});

Beware that the example I gave you is using the JSONAPI Adapter (Ember 2.0) but there's also a RESTAdapter, you have to choose the right one for you. 请注意,我给您的示例正在使用JSONAPI适配器(Ember 2.0),但是还有一个RESTAdapter,您必须为您选择合适的示例。

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

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