My array is made-up of multiple objects like this:
{
"id": 2,
"nom_operation": "opSavCreationTest2",
"num_serie": "1203250cd81b2101",
"address_mac": "04:15:d9:00:00:49",
"description": "opSavCreationTest2",
"adminId": 82,
"produitId": 1,
"clientId": 2,
"status": "produit_recu",
"lieu_achat_produit": "opSavCreationTest2",
"garanti_au": "07-02-2021",
"createdAt": "2020-03-27T15:15:55.207Z",
"updatedAt": "2020-03-27T15:15:55.207Z",
"produit": {
"id": 1,
"nom_produit": "PURE BLACK MAT",
"type_produit": "Pure",
"couleur_produit": "black_mat",
"nbr_op_sav_en_cours": 7,
"img": "img",
"createdAt": "2020-03-30T08:41:39.597Z",
"updatedAt": "2020-03-30T15:37:09.294Z"
},
"client": {
"id": 2,
"nomClient": "opSavCreationTest2",
"numTel": "5775757575",
"email": "opSavCreationTest2",
"addresse": "opSavCreationTest2",
"ville": "opSavCreationTest2",
"pays": "opSavCreationTest2",
"createdAt": "2020-03-27T15:15:55.043Z",
"updatedAt": "2020-03-27T15:15:55.043Z"
},
"admin": {
"id": 82,
"nom": "administrateur",
"email": "aeaffeef@gmail.com",
"mot_de_passe": "efefr3lnr8Fr4IwEsc=",
"last_login": null,
"role": "administrateur",
"createdAt": "2020-03-23T09:08:43.585Z",
"updatedAt": "2020-03-23T09:08:43.585Z"
}
}
Something strange happens when I try to access the values inside the nested object produit by iterating over the array with map
this.state.operationSavInformation.map(
(operationSavInformation, i) => {
const operationSavLink = `/home/operationSav/${operationSavInformation.id}`;
console.log("operationSavInformation ", i);
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
/** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT"
* type_produit: "Pure"
* couleur_produit: "black_mat"
* nbr_op_sav_en_cours: 7
* img: "img"
* createdAt: "2020-03-30T08:41:39.597Z"
* updatedAt: "2020-03-30T15:37:09.294Z"
*/
console.log(
operationSavInformation.produit["nom_produit"]
); //ERROR
/**TypeError: Cannot read property 'nom_produit' of undefined */
When I am trying to access the value of the property 'nom_produit' of produit, I get this error:
TypeError: Cannot read property 'nom_produit' of undefined
Even though, I have succesfully logged the nested object produit !!
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
/** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT"
* type_produit: "Pure"
* couleur_produit: "black_mat"
* nbr_op_sav_en_cours: 7
* img: "img"
* createdAt: "2020-03-30T08:41:39.597Z"
* updatedAt: "2020-03-30T15:37:09.294Z"
*/
I really couldn't find an explanation for this strange result. If produit object is logged succesfully why is it treated as undefined when I try to access one of its properties?
This is absolutely ridiculous. I added an if else
, and all of the sudden it works:
this.state.operationSavInformation.map(
(operationSavInformation, i) => {
const operationSavLink = `/home/operationSav/${operationSavInformation.id}`;
console.log("operationSavInformation ", i);
console.log("operationSavInformation.produit"); // Works
console.log(operationSavInformation.produit);
const produit = operationSavInformation.produit;
console.log(
"produit = operationSavInformation.produit;"
);
console.log(produit);
if (produit !== undefined) {
// This gets executed despite the error without if else says that produit is undefined!!
console.log("produit is not undefined");
console.log(produit.nom_produit);
} else {
// This doesn't get executed despite the error without the if else is that it cannot read nom_produit of undefined
console.log("produit is undefined");
}}
If I remove that condition however, I still get the produit is undefined error but the condition checks that it is not undefined and it works!
By the log you show:
** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT" // " nom_produit" should be right
* type_produit: "Pure"
I guess, you miss a space of this attribute, try
console.log(operationSavInformation.produit[" nom_produit"])
const test = { "id": 2, "nom_operation": "opSavCreationTest2", "num_serie": "1203250cd81b2101", "address_mac": "04:15:d9:00:00:49", "description": "opSavCreationTest2", "adminId": 82, "produitId": 1, "clientId": 2, "status": "produit_recu", "lieu_achat_produit": "opSavCreationTest2", "garanti_au": "07-02-2021", "createdAt": "2020-03-27T15:15:55.207Z", "updatedAt": "2020-03-27T15:15:55.207Z", "produit": { "id": 1, " nom_produit": "PURE BLACK MAT", //You should check this attribute, is it " nom_produit" or "nom_produit" "type_produit": "Pure", "couleur_produit": "black_mat", "nbr_op_sav_en_cours": 7, "img": "img", "createdAt": "2020-03-30T08:41:39.597Z", "updatedAt": "2020-03-30T15:37:09.294Z" }, "client": { "id": 2, "nomClient": "opSavCreationTest2", "numTel": "5775757575", "email": "opSavCreationTest2", "addresse": "opSavCreationTest2", "ville": "opSavCreationTest2", "pays": "opSavCreationTest2", "createdAt": "2020-03-27T15:15:55.043Z", "updatedAt": "2020-03-27T15:15:55.043Z" }, "admin": { "id": 82, "nom": "administrateur", "email": "aeaffeef@gmail.com", "mot_de_passe": "efefr3lnr8Fr4IwEsc=", "last_login": null, "role": "administrateur", "createdAt": "2020-03-23T09:08:43.585Z", "updatedAt": "2020-03-23T09:08:43.585Z" } } console.log(test.produit[" nom_produit"]) console.log(test.produit["nom_produit"])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.