我的数组由如下多个对象组成:
{
"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"
}
}
当我尝试通过使用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 */
当我尝试访问产品的属性'nom_produit'的值时,出现此错误:
TypeError:无法读取未定义的属性'nom_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"
*/
我真的找不到这种奇怪结果的解释。如果成功记录了产品对象,为什么在我尝试访问其属性之一时将其视为未定义?
答案 0 :(得分:0)
这绝对是荒谬的。
我添加了if else
,突然之间一切正常:
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");
}}
但是,如果我删除该条件,我仍然会收到产品未定义错误,但是条件会检查它是否未定义并且可以工作!
答案 1 :(得分:-1)
通过显示的日志:
this.filteredOptions = this.userService
.getUserSuggestions(this.displayName, 30)
.pipe(
map(response => someVariable ? response.filter(option => option.eMail != null) : response),
debounceTime(400),
distinctUntilChanged()
);
我想,您可能会错过此属性的空格,请尝试
** LOGS THIS:
*
* id: 1
* nom_produit: "PURE BLACK MAT" // " nom_produit" should be right
* type_produit: "Pure"
console.log(operationSavInformation.produit[" nom_produit"])