Hii:我试图通过来自api的id显示单个数据,这是我到目前为止所拥有的,
api get方法:
INSERT INTO users;
compo.ts
OK
这是service.ts
app.get('/movies/:id', (req, res) => {
const id =req.params.id;
request('https://api.themoviedb.org/3/movie/'+id+'?&api_key=2931998c3a80d7806199320f76d65298', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred and handle it
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
res.send(body)
});
});
这里是compo html:
import { Component, OnInit} from '@angular/core';
import { MoviesService } from '../movies.service';
import { RouterModule, Routes } from '@angular/router';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-movie',
templateUrl: './movie.component.html',
styleUrls: ['./movie.component.scss']
})
export class MovieComponent implements OnInit {
movie: object;
constructor(private router: ActivatedRoute, private moviesService: MoviesService){}
ngOnInit() {
this.router.params.subscribe((params) => {
// tslint:disable-next-line:prefer-const
let id = params['id'];
this.moviesService.getMovies(id)
.then((movie: any) => {
console.log(movie);
this.movie = movie.results;
});
});
}
}
}
邮递员测试数据的最后一个需要的结构:
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Jsonp } from '@angular/http';
@Injectable({
providedIn: 'root'
})
export class MoviesService {
moveUrl = '/movies/';
private apiUrl = 'http://localhost:8000';
constructor(private http: Http, private _jsonp: Jsonp) { }
getMovie(id: string): Promise<any> {
return this.http.get(this.apiUrl + 'moveUrl')
.toPromise()
.then(this.handleData)
.catch(this.handleError);
}
}
当我运行我的应用程序时,前端没有显示数据我在这里做错了什么?或者我需要改变什么,只是学习伙伴:)谢谢
答案 0 :(得分:1)
我想我知道错误是什么。
在getMovies
中你有这个:
return this.http.get(this.apiUrl + 'moveUrl')
可是:
id
参数。因此,最后,无论id
是哪一个,您都向http://localhost:8000moveUrl
发出请求。这不是有效的网址,因此是您收到错误的原因。
将代码更改为:
return this.http.get(this.apiUrl + moveUrl + id)
.toPromise()
.then(this.handleData)
.catch(this.handleError);
这样您就可以向http://localhost:8000/movies/{id}
发出请求,这就是您想要的。
但是你必须学会使用网络工具。如果你完成了我的要求,你自己就会看到错误。