angular2路由与快递

时间:2015-12-15 11:36:53

标签: mysql node.js express angular

我使用的是ng2 alpha 42版本

我的代码如下:

问题是我期望输出中只有一个数据,所以我不需要在我的jade.code中使用(ng-for),但是我们可以看到意想不到的结果,我该如何更改,请帮助!

表达(当我在PostMan中检查API时它起作用)

router.get('/project/:id', function(req,res){
sql = "SELECT * FROM Projects WHERE id=" + "'" + req.params.id + "'"; 
db.query(sql,function(err,project){
    if(err){
        res.send(err);
    }else{
        res.json(project)
        console.log(project) 

    }
  })
})

ng2代码(不工作)

export class Project{
id: string;
project: string;
constructor(public routeParams : RouteParams, 
            http:Http){
    //this.projects = [];
    this.id = routeParams.get('id')

    http.get('/api/projects/project/' + this.id)
        .map(res => res.json())
        .subscribe(
            project => this.project = project


        )

}
}

玉码(模板引擎)

li(*ng-for="#a of project")
  h1 {{ a.title}}
  h1 {{ a.author}}

h1 {{project}}

结果:

first topic
author1
[object Object]

我预计玉器中的代码是:

h1 {{ project.title}}
h2 {{ project.author}}

但如果我把它,我的控制台出错:

EXCEPTION: TypeError: Cannot read property 'title' of undefined in [null]

解决此问题

在快递中,这只召回一个数据

res.json(project[0])

在ng2中,我忘记声明项目类型

this.project = {};

这很有效。如果你能给我任何建议我会感激不尽。

0 个答案:

没有答案
相关问题