简体   繁体   English

尝试访问数组对象中的属性时未定义

[英]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.

相关问题 尝试访问 object 的属性时出现“TypeError:无法读取未定义的属性” - Getting "TypeError: Cannot read properties of undefined" when trying to access properties of an object 尝试访问 javascript 中的 object 属性时出现“未定义” - Getting "undefined" when trying to access an object property in javascript 尝试访问数组中的对象时,grep返回未定义 - grep returning undefined when trying to access object in array 尝试访问嵌套在数组中的 object 的属性时未定义的类型 - Typeof undefined when trying to access property of object nested in array 尝试访问ReactJS中嵌套在对象中的数组时获取未定义 - Get Undefined when trying to access array nested in Object in ReactJS Javascript对象数组访问属性未定义? - Javascript Object Array access properties undefined? JavaScript - 尝试访问数组但未定义 - JavaScript - Trying to access an array but getting undefined 尝试使用 object 属性中的值填充字符串 - 获取“未定义” - Trying to populate string with values from object properties - getting "undefined" 尝试使用 Array.prototype.filter 访问嵌套的 object 字段时出现未定义错误 - Getting undefined error trying to access nested object field using Array.prototype.filter 在js中的原型继承期间,当我尝试访问继承对象的方法时,我变得未定义 - during prototypal inheritance in js, i am getting undefined when i am trying to access the method of inherited object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM