我的GET路线有问题。正如标题所说。 Html代码:
<ion-card *ngFor="let room of rooms">
组件:
rooms : any;
this.rooms = this.roomsService.getRooms();
提供者:
getRooms(){
return new Promise(resolve => {
this.http.get('http://localhost:8080/api/rooms/')
.map(res => res.json())
.subscribe(data => {
resolve(data);
});
});
}
服务器端:
exports.getRooms = function (req, res) {
Room.find({}, function (err, rooms) {
if (err) {
res.send(err);
} else {
res.json(rooms);
}
});
}
我做错了什么?
返回json的结构:
[ { _id: 58eb65dcfd9b3c188c74d811,
room_number: 1,
type: 'standard',
beds: 1,
max_occupancy: 1,
cost_per_night: 50,
__v: 0,
reserved: [] },
{ _id: 58eb6608fd9b3c188c74d812,
room_number: 2,
type: 'standard',
beds: 1,
max_occupancy: 1,
cost_per_night: 55,
__v: 0,
reserved: [] } ]
答案 0 :(得分:0)
我以非常简单的方式解决了这个问题。有一些问题:
this.rooms = this.roomsService.getRooms();
因为我不能直接分配这样的承诺,所以我做了:
this.roomsService.getRooms().then((data) => {
this.rooms = data;
}, (err) => {
console.log(err);
});