我正在尝试使用expo反应原生语从手机中获取联系号码。访问联系人对象后。电话号码在phoneNumber数组中。该数组有一系列对象,每个对象都有数字键中的数字。我的动作创建者将在下面将数据存储在数据库中。我稍后会将其更改为本地存储。 https://docs.expo.io/versions/latest/sdk/contacts.html#content
export const setContacts = (contacts) => async dispatch => {
// set the contacts object into firebase and load it into redux
// contacts is an array of objects; convert it object with key phone number
// key should be phoneNumbers; phoneNumbers is an array of phone numbers
// function below converts array to object to save to database with key for each object
// whenever getting information outside app validate and format for storage into db
console.log(contacts, 'this is the contacts object inside setContacts action');
// filter contacts that don't have phoneNumber
const arrayToObject = (array, keyField) =>
array.filter(item => item[keyField].length !== 0)
.map(item2 => {
const item3 = item2;
item3.keyField = item2.keyField
.filter(item => item.digits.length === 10 || item.digits.length === 11)
.map(item4 => {
const item5 = item4;
item5.digits = item4.digits.length === 11 ?
item4.digits.substr(1) : item4.digits;
return item5;
});
return item3;
})
.reduce((object, item) => {
const accumulator = object;
accumulator[`${item[keyField][0].digits}`] = item.name;
return accumulator;
}, {});
// object of objects to save in db
const phoneNumbers = 'phoneNumbers';
const contactsObject = arrayToObject(contacts, phoneNumbers);
console.log(contactsObject, 'this is the contactsObject inside setContacts action');
// save into firebase
const user = await firebase.auth().currentUser;
if (user !== null) {
await firebase.database().ref(`/users/${user.uid}/contacts`)
.set(contactsObject)
.then(() => console.log('contactsObject set into /users/uid/contacts inside setContacts action'))
.catch((e) => console.error(e));
console.log(contactsObject, 'contactsObject inside setContacts action before dispatch');
dispatch({ type: CONTACTS, payload: contactsObject });
} else {
console.log('user object was not found inside setContacts action');
}
};
上面的代码不起作用。我认为我的逻辑特别是arraytoObject函数有错误。我只需要帮助修理鳕鱼