Node.js Angular2API调用不返回任何内容
我的问题是,它不是以html格式显示的。我怎样才能解决这个问题? 查询很好,我在URL上得到了结果,但无法在component.html上显示它。 (它可以工作,我可以查看是否调用URL/api/mainstorage以便显示JSON内容。) Index.jsNode.js Angular2API调用不返回任何内容,node.js,angular,api,Node.js,Angular,Api,我的问题是,它不是以html格式显示的。我怎样才能解决这个问题? 查询很好,我在URL上得到了结果,但无法在component.html上显示它。 (它可以工作,我可以查看是否调用URL/api/mainstorage以便显示JSON内容。) Index.js var express = require('express'); var router = express.Router(); // http://localhost:3000/ router.get('/',
var express = require('express');
var router = express.Router();
// http://localhost:3000/
router.get('/', function(req, res, next) {
res.status(200)
.json({
status: 'success',
message: 'Live long and prosper!'
});
});
var db = require('./queries');
router.get('/api/mainstorage', db.getAllDocuments);
module.exports = router;
querys.js
var promise = require('bluebird');
var options = {
// Initialization Options
promiseLib: promise
};
var pgp = require('pg-promise')(options);
var connectionString ='postgres://dbuser:Storage@localhost/mainstorage'
var db = pgp(connectionString);
const axios = require('axios');
const API = 'http://localhost:3000';
function getAllDocuments(req, res, next) {
axios.get(`${API}/main`)
db.any('SELECT * FROM files')
.then(function (data) {
res.status(200)
.json({
status: 'success',
data: data,
message: 'Retrieved all files'
});
})
.then(documents => {
res.send(200).json();
})
.catch(function (err) {
return next(err);
});
}
module.exports = {
getAllDocuments: getAllDocuments
};
文件.component.ts
export class DocumentsComponent implements OnInit {
title = 'app works!';
mainstorage;
documents: any [];
constructor(private documentsService: DocumentsService) { }
ngOnInit() {
// Retrieve documents from the API
this.documentsService.getAllDocuments().subscribe(documents => {
this.documents = documents;
});
}
}
文件.service.ts
@Injectable()
export class DocumentsService {
constructor(private http: Http) {}
getAllDocuments(){
return this.http.get('/api/mainstorage')
.map(res => res.json());
}
}
documents.component.html
<div class="row" *ngFor="let document of documents">
<div class="card card-block">
<h4 class="card-title">{{ documents.id }}</h4>
<p class="card-text">{{document.country}}</p>
{{documents.id}
{{document.country}
您无法在html中看到任何内容,因为服务数据是异步的,您试图在服务返回之前显示它。
您可以通过将变量包装在*ngIf
<div *ngIf='documnets'>
<div class="row" *ngFor="let document of documents">
<div class="card card-block">
<h4 class="card-title">{{ documents.id }}</h4>
<p class="card-text">{{document.country}}</p>
</div>
</div>
</div>
{{documents.id}
{{document.country}
*ngIf
将检查是否有文档
,一旦收到来自服务的数据,它将显示出来 您可以登录以检查数据是否出现在服务调用中吗?我们也使用您建议的模式。我想知道,*ngFor=“让文档中的文档|异步”
是否完成了同样的任务?