获取Angular 2的单个文档

获取Angular 2的单个文档

问题描述:

我正在关注angular 2 Tutorial of Heroes指南,并试图从mongoDb中获取并显示一个英雄详细信息部分(如教程中所示)。我能够在mongo中找到我的用户(英雄)运行节点后端,但在访问该节时却不是单个英雄。 我对typeScript完全陌生,所以请随身携带。获取Angular 2的单个文档

hero.service.ts

//The part that gives me errors. 
private heroesUrl = 'http://localhost:8080/admin/users'; 

constructor(private http: Http) { } 

    //Works 
    getHeroes() { 
    return this.http.get(this.heroesUrl) 
     .toPromise() 
     .then(response => response.json() as Hero[]) 
     .catch(this.handleError); 
    } 

    //Doesn't work  
    getHero(_id: number) { 
    return this.getHeroes() 
     //How should I work with the _id? 
     .then(heroes => heroes.find(hero => hero._id === _id)); 
    } 
    //:note that this worked properly when I had the documents in memory with a mock api. Where the ids initially where numbers. 

在我看来,该ID必须是数字:

https://angular.io/docs/ts/latest/tutorial/toh-pt5.html

The /detail/ part of that URL is constant. The trailing numeric id part changes from hero to hero. We need to represent that variable part of the route with a parameter (or token) that stands for the hero's id.

,而且我的ObjectId得到的MongoDB不与此合作很好。如果我将number参数改为字符串,并将id解析为字符串,它也不起作用,我也没有料到它或者因为我现在不知道我在做什么!

getHero(_id: String) 

反正...当我跑我的打字稿编译我得到这个错误:

app/+heroes/hero.service.ts(24,65): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. 

我真的不明白这是如何工作的角2 /打字稿,遗憾的是不知道要么就我的问题要求什么。

app.routes.ts

const routes: RouterConfig = [ 
    { 
    path: '', 
    component: DashboardComponent 
    }, 
    { 
    path: 'detail/:id', 
    component: HeroDetailComponent 
    }, 
    { 
    path: 'heroes', 
    component: HeroesComponent 
    } 
]; 

我的应用程序识别ID,并将我的正确路径:http://localhost:8080/detail/5773fbc1383ec4868619f1fa 不过,虽然在页面上没有找到数据。

Failed to load resource: the server responded with a status of 404 (Not Found) 

在我的仪表板中,这是我如何导航到英雄自己的路线。从实例教程

export class DashboardComponent implements OnInit { 

    heroes: Hero [] = []; 

    constructor(
    private router: Router, 
    private heroService: HeroService) { 
    } 

    ngOnInit() { 
    this.heroService.getHeroes() 
     .then(heroes => this.heroes = heroes); 
    } 

    gotoDetail(hero: Hero) { 
    let link = ['/detail', hero._id]; 
    this.router.navigate(link); 
    } 
} 

一切期望返回的数据的ID是数字。如果我改变的数值是字符串,而不是在..

//hero.ts 
    export class Hero { 
    _id: number; 
    name: string; 
    } 

和所有其他地方发生这种情况,我收到了一堆错误,我不能显示的每一个地方,这将导致得多例子码。

我的REST API的节点: (这可以作为使用邮差时预期)

//server/routes/admin.js 

var express = require('express'); 
var app = express(); 
var router = express.Router(); 
var passport = require('passport'); 
var User = require('../models/user'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var session = require('express-session'); 


router.get('/users', /*isAdmin,*/ function(req, res) { 
    User.find(function(err, users){ 
     if (err) 
     return res.send(err); 
     res.json(users); 
    }); 
    }); 
    //CRUD operations for a single Company by its id 
    router.route('/users/:id') 
    .get(/*isAdmin, */function(req, res){ 
     User.findById(req.params.id, function(err, user){ 
     if (err) 
      res.send(err); 
     res.json(user); 
     }); 
    }) 

我将如何做才能找到一个mongodb的单个文档相对于它的_id从客户的角度?任何可能使我指向正确方向的东西。

谢谢!

我想有一个特定的HTTP路由来检索单个英雄/文档。下面是它应该如何在客户端来完成:

getHero(_id: number) { 
    return this.http.get(this.heroesUrl + '/' + _id) 
     .toPromise() 
     .then(response => response.json() as Hero) 
     .catch(this.handleError); 
} 

我想你应该有一个路线:

router.get('/users/:id', /*isAdmin, */function(req, res){ 
    User.findById(req.params.id, function(err, user){ 
    if (err) 
     res.send(err); 
    res.json(user); 
    }); 
}) 
+0

这似乎是合理的,但遗憾的是它并没有为我做的伎俩。 –

+0

好的,我刚刚读了你的教程,这是在内存数据库中,所以你似乎没有处理一个真正的web API。但是,由于您使用本地主机url,我想您已经完成了节点REST API。你能告诉我们你的节点API的代码吗?我认为它错过了一些东西。 – Odonno

+0

是的,这是正确的。正如我所说,我能够让所有用户从我的REST API返回,但不能为单个用户返回。我将确保添加服务器片段。 –