[英]Getting undefined when trying to access properties in array object
I have been stuck for two days trying to access the properties of an array object.我被困了两天试图访问数组对象的属性。 I can see in console that the properties exist, but for some reason I can't access them.
我可以在控制台中看到这些属性存在,但由于某种原因我无法访问它们。
I am loading a users contacts when component mounts/useEffect using React-Native, and then I pass them to a function to filter and return only the phoneNumbers.当组件使用 React-Native 安装/useEffect 时,我正在加载用户联系人,然后我将它们传递给一个函数以过滤并仅返回电话号码。
Here is the async function which load the contacts, setState, and make asynchronous backend calls:这是加载联系人、setState 并进行异步后端调用的异步函数:
async function getAvailableContacts(contactArray) {
try {
const phoneNumbers = filterPhoneNumbers(contactArray);
const contactsWhoUseApp = await axios.post(
`${process.env.REACT_APP_backend_url}/available-contacts`,
{ phoneNumbers }
);
const newContacts = await getContacts(
contactArray,
contactsWhoUseApp.data
);
setRenderedContacts((prev) => newContacts);
} catch (error) {
console.log(error);
}
}
useEffect(() => {
(async () => {
const { status } = await Contacts.requestPermissionsAsync();
if (status === "granted") {
const { data } = await Contacts.getContactsAsync({
fields: [Contacts.Fields.PhoneNumbers],
});
if (data.length > 0) {
setContactArray((prev) => data);
getAvailableContacts(contactArray);
}
}
})();
}, []);
This is the function I pass the array to:这是我将数组传递给的函数:
export function filterPhoneNumbers(contacts) {
let arrOfPhoneNumbers = [];
for (let i = 0; i < contacts.length; i++) {
console.log(contacts[i].phoneNumbers[0]);
}
return arrOfPhoneNumbers;
}
When I console.log contacts[i]
this is what I see:当我 console.log
contacts[i]
这就是我所看到的:
Object {
"contactType": "person",
"firstName": "none",
"id": "123",
"imageAvailable": false,
"lastName": "none",
"lookupKey": "1234567",
"name": "none none",
"phoneNumbers": Array [
Object {
"id": "12",
"isPrimary": 1,
"label": "mobile",
"number": "+9999999999",
"type": "2",
},
Object {
"id": "12345",
"isPrimary": 0,
"label": "mobile",
"number": "+9999999999",
"type": "2",
},
],
}
But I get this error when trying to access phoneNumbers properties:但是在尝试访问 phoneNumbers 属性时出现此错误:
undefined is not an object (evaluating 'contacts[i].phoneNumbers[0]')
However, when I console.log and remove the [0]
from phoneNumbers[0]
I can see the phoneNumbers array:但是,当我 console.log 并从
phoneNumbers[0]
中删除[0]
时,我可以看到 phoneNumbers 数组:
Array [
Object {
"id": "123",
"isPrimary": 1,
"label": "mobile",
"number": "+9999999999",
"type": "2",
},
Object {
"id": "1234",
"isPrimary": 0,
"label": "mobile",
"number": "+9999999999",
"type": "2",
},
]
Anyone ever had a similar issue and knows what I am doing wrong?任何人都遇到过类似的问题并且知道我做错了什么?
I can't see how your code is throwing that error, unless you are putting undefined in your contacts array somewhere.我看不出你的代码是如何抛出这个错误的,除非你把 undefined 放在你的联系人数组中的某个地方。
You don't actually pass the contacts to your filter function.您实际上并没有将联系人传递给您的过滤器功能。
// Schedule a re-render where contactArray will become data
setContactArray((prev) => data);
// Send the OLD contactArray into getAvailableContacts
// since it hasn't been updated yet
getAvailableContacts(contactArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.