I am trying to get some data from my API
in react using axios.
I keep getting this error Uncaught TypeError: Cannot read properties of undefined (reading 'id')
whenever i try doing something like order.service.id
, if i am on the service detail page and log service.id
, it displays the id
in the console, now, i am in the order detail page and trying to fetch the service that the user have ordered in the order page and i am trying to get the service id from the order like this order.service.id
object because the order have a foreignkey to the service
, but it keep showing the undefined error.
const {order_id} = useParams();
const [order, setOrderDetail] = useState([])
useEffect(() => {
const fetchOrderDetail = async () => {
await api.get(baseUrl + '/buyer-order-detail/' + user_id + '/' + order_id).then((res) => {
setOrderDetail(res.data)
})
}
fetchOrderDetail()
}, [])
console.log(order.id) // 31
console.log(order.price) // $100.00
console.log(order.service.id) // undefined
console.log(order.service.title) // undefined
//Hence the request below does not run because `order.service.id` is undefines.
axios.get(baseUrl + '/fetch-review-status/' + order.service.id + '/' + user_id)
This always happens when the chaining (order.id)
starts getting nested (chained) more and more, e.g order.service.title
or order.service.user.full_name
This is the Order Detail Full Json Objects
{
"id": 31,
"service": {
"id": 1,
"sid": "hpnqaofesz",
"image": "http://127.0.0.1:8000/media/service-images/young-sports-lady-beach-make-meditation-exercises.jpg",
"title": "Mediation",
"description": "<p>Mediation</p>",
"price": "20.00",
"tags": null,
"features": null,
"address": "Ulleja Street",
"longitude": "21330897654",
"latitude": "41230897651",
"views": 196,
"status": "live",
"date": "2023-03-11T09:43:12.530767Z",
"user": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"category": {
"id": 1,
"cid": "nstan",
"image": "http://127.0.0.1:8000/media/category.jpg",
"title": "Health",
"slug": "Health",
"description": "",
"date": "2023-03-11T09:42:49.447683Z"
},
"liked": []
},
"description": "Limitless Learning For Everyone",
"phone": "+2348091744974",
"seller": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"buyer": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"buyer_profile": {
"pid": "58394133406067328223",
"user": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"full_name": "Destiny Franks",
"bio": "Software Enginner",
"country": "Nigeria",
"image": "http://127.0.0.1:8000/media/user_images/13891866_SL_0212121_40670_78.jpg",
"address": "Ulleja Street",
"phone": "+2348091744974",
"website": "https://cwivel.com/",
"pin": "6387",
"facebook": "https://facebook.com/",
"instagram": "https://instagram.com/",
"twitter": "https://twitter.com/",
"whatsApp": "+123 (456) 789",
"verified": false,
"wallet": "0.00",
"saved_service": [
{
"id": 1,
"sid": "hpnqaofesz",
"image": "http://127.0.0.1:8000/media/service-images/young-sports-lady-beach-make-meditation-exercises.jpg",
"title": "Mediation",
"description": "<p>Mediation</p>",
"price": "20.00",
"tags": null,
"features": null,
"address": "Ulleja Street",
"longitude": "21330897654",
"latitude": "41230897651",
"views": 196,
"status": "live",
"date": "2023-03-11T09:43:12.530767Z",
"user": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"category": {
"id": 1,
"cid": "nstan",
"image": "http://127.0.0.1:8000/media/category.jpg",
"title": "Health",
"slug": "Health",
"description": "",
"date": "2023-03-11T09:42:49.447683Z"
},
"liked": []
}
],
"times_saved": 0,
"code": "REF1e4j5eh3d3",
"recommended_by": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"total_rating": 0,
"total_service": 1,
"total_orders": 1,
"total_cash_spent": 100,
"weekly_expenses": 100,
"followers": [
{
"id": 1,
"username": "destiny",
"email": "desphixs@gmail.com"
}
],
"followings": [
{
"id": 1,
"username": "destiny",
"email": "desphixs@gmail.com"
}
],
"date": "2023-03-11T09:06:40.886952Z"
},
"seller_profile": {
"pid": "58394133406067328223",
"user": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"full_name": "Destiny Franks",
"bio": "Software Enginner",
"country": "Nigeria",
"image": "http://127.0.0.1:8000/media/user_images/13891866_SL_0212121_40670_78.jpg",
"address": "Ulleja Street",
"phone": "+2348091744974",
"website": "https://cwivel.com/",
"pin": "6387",
"facebook": "https://facebook.com/",
"instagram": "https://instagram.com/",
"twitter": "https://twitter.com/",
"whatsApp": "+123 (456) 789",
"verified": false,
"wallet": "0.00",
"saved_service": [
{
"id": 1,
"sid": "hpnqaofesz",
"image": "http://127.0.0.1:8000/media/service-images/young-sports-lady-beach-make-meditation-exercises.jpg",
"title": "Mediation",
"description": "<p>Mediation</p>",
"price": "20.00",
"tags": null,
"features": null,
"address": "Ulleja Street",
"longitude": "21330897654",
"latitude": "41230897651",
"views": 196,
"status": "live",
"date": "2023-03-11T09:43:12.530767Z",
"user": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"category": {
"id": 1,
"cid": "nstan",
"image": "http://127.0.0.1:8000/media/category.jpg",
"title": "Health",
"slug": "Health",
"description": "",
"date": "2023-03-11T09:42:49.447683Z"
},
"liked": []
}
],
"times_saved": 0,
"code": "REF1e4j5eh3d3",
"recommended_by": {
"id": 1,
"password": "pbkdf2_sha256$260000$1o9WBXwtcSeveI4ZWiprQm$+0U0ZSIUUXwvEe30yto2mSxsAv85c6iM2wm7wOeVRnE=",
"last_login": "2023-03-18T14:18:52.953050Z",
"is_active": true,
"date_joined": "2023-03-11T09:06:40.400367Z",
"username": "destiny",
"first_name": "",
"last_name": "",
"image": "http://127.0.0.1:8000/media/default.jpg",
"wallet": "0.00",
"verified": false,
"email": "desphixs@gmail.com",
"is_staff": true,
"is_superuser": true,
"groups": [],
"user_permissions": []
},
"total_rating": 0,
"total_service": 1,
"total_orders": 1,
"total_cash_spent": 100,
"weekly_expenses": 100,
"followers": [
{
"id": 1,
"username": "destiny",
"email": "desphixs@gmail.com"
}
],
"followings": [
{
"id": 1,
"username": "destiny",
"email": "desphixs@gmail.com"
}
],
"date": "2023-03-11T09:06:40.886952Z"
},
"booking_date": "2023-03-17",
"booking_time": "14:01:39",
"status": "delivering",
"duration": "1_week",
"date": "2023-03-17T12:29:58.241805Z",
"payment_status": "paid",
"price": "100.00"
}
I would provide more code and information if needed