Lỗi respond data
-
Mình viết hàm lấy API JSON dùng Promise (resolve, reject) mà cứ bị reject hoài không hiểu lý do tại sao. Anh em check lỗi giúp mình nha. Tạ ơn anh em nhiều lắm
File JSON: {"id":3,"url":"http://localhost:8000/api/v1/feeds/3/?format=json","user":{"id":2,"username":"lecongtoan2","first_name":"Tran","last_name":"Haong"},"date":"2017-08-31T04:56:32.785000Z","post":"lecongtoan2 has joined the network.","likes":0,"comments":0,"comment_list":[]}
getFeedDetail (id) {
baseUrl: 'http://localhost:8000/api/v1/',
return new Promise((resolve, reject) => {
request.defaults.baseURL = this.baseUrl
request.get(feeds/${id}/
).then(response => {
const data = [...response.data][0]
if (response.status === 200 && response.data.length > 0) {
const filtered = {
post: data.post
}
resolve(filtered)
} else {
console.log('Failed')
reject(response)
}
})
})
}
} -
@Công-Toàn-Lê
Đoạn code bạn gởi bị lỗi cú pháp nhiều quá. Bạn nên gởi đoạn code chính xác để người khác còn giúp được. -
Mình nghi ngờ vấn đề nằm ở chỗ này:
baseUrl: 'http://localhost:8000/api/v1/', return new Promise((resolve, reject) => {
Bạn thử đổi thành:
baseUrl = 'http://localhost:8000/api/v1/'; return new Promise((resolve, reject) => {
Thử xem code có chạy không nhé
-
Cảm ơn các bạn. mình đã fix dc rồi nha