get contacts from phone using react native expo - javascript

I am trying to get contact numbers from the phone using expo react native. after accessing the contacts object. The phone numbers are in the phoneNumber array. The array has a series of objects and each object has the digits in the digits key. My action creator is below that will store the data in database for now. I will change it to local storage later.
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');
}
};
the above code doesn't work. I think there is error in my logic specifically the arraytoObject function. i just need help with fixing the cod

Related

How to find note by id in firebase?

I have action in vuex. This action do request to firebase.
I would like to receive record from firebase by 'id'.
But I don't know where I must input this 'id' for searching in database.
I did it here:
const snapshot = await get(child(dbRef, `/users/${uid}/records`), id);
but I receive all records, without filtration by 'id'.
async fetchRecordById({ dispatch, commit }, id) {
try {
let record
const uid = await dispatch('getUid')
const dbRef = ref(getDatabase());
const snapshot = await get(child(dbRef, `/users/${uid}/records`), id);
record = snapshot.val()
if (record === null) {
record = {}
}
return {...record, id}
} catch (e) {
commit('setError', e)
throw e
}
},
For version 8 firebase it looks like:
const record = (await firebase.database().ref(`/users/${uid}/records`).child(id).once('value')).val() || {}
How to integrate in version 9?
The get() function only takes one DatabaseReference as a parameter but you are also passing the id.
Instead, you can specify the path directly in ref() function as shown below:
console.log(uid, id) // <-- check if values are correct
const dbRef = ref(getDatabase(), `/users/${uid}/records/${id}`)
const snapshot = await get(dbRef);
console.log(snapshot.val())

.filter() not filtering the items from the array

I have this array of data in Javascript:
// console.log(currentMembers.members)
// *************************
// CURRENT MEMBERS
// *************************
// [
// new ObjectId("62385d8caee17d13a1762b39"),
// new ObjectId("6238a480170aff10d16ccd86"),
// new ObjectId("6238a480170aff10d16ccd86"),
// new ObjectId("6238a608170aff10d16ccd89")
// ]
I want to remove from the array one value that matches the variable "memberToRemove". So .filter() should be enough to perform this but it doesn't and I'm lost.
try {
const newListofMembers = currentMembers.members.filter(
member => member._id !== memberToRemove
);
const updatedMembers = await Group.findOneAndUpdate(
{ _id: groupId },
{ members: newListofMembers }
);
console.log('Users successfully updated.');
return res.status(200).json({ success: true, members: newListofMembers });
} catch (err) {
console.log(err);
next(err);
}
When I perform this action nothing happens, when I console.log(newListOfMembers) the filter doesn't seem to work at all, it ignores the member => member !== memberToRemove. The member to remove is 6238a608170aff10d16ccd89.
Simplest, you can change you filter into this member => String(member._id) === memberToRemove
But the right way is
const _ = require('lodash');
const { Types } = require('mongoose');
const newListofMembers = currentMembers.members.filter(
member => !_.isEqual(member._id, Types.ObjectId(memberToRemove))
);
You can replace member => !_.isEqual(member._id, Types.ObjectId(memberToRemove)) with member => !_.isEqual(member, Types.ObjectId(memberToRemove)) or !item.equals(memberToRemove)

I am trying to get a users from firestore and store them in Map in javascript but for some reasone the map is empty

So the user Object has 5 attributes firstName , lastName,email,password and city.
I am getting the users with a method in class Users which is :
getUsersData () {
const usersMap = new Map()
this.users.get().then((data) => {
data.docs.forEach(user=> {
const {email , password} = user.data();
usersMap.set(email,password);
});
});
return usersMap;
}
which returns this in chrome console:
Map(0) {}
[[Entries]]
0: {"mehdiboujid#gmail.com" => "dqdqwdq"}
size: (...)
__proto__: Map
I am trying to use credentials in a Map because every user will have a unique email which will be the key to the map.
If this.users.get() is async you will have to await the result before returning the usersMap.
async getUsersData() {
const usersMap = new Map()
await this.users.get()
.then((data) => {
data.docs.forEach(user => {
const { email, password } = user.data();
usersMap.set(email, password);
});
});
return usersMap;
}

Firebase Cloud Function error: Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array

I want to send a notification to all users who are confirmed guests when the object confirmedGuests is created in the Firebase Realtime Database.
So, I first create an array of all the users from confirmedGuests object. Then, I iterate through all these users and push their deviceTokens to an array of deviceTokens. The array allDeviceTokens is expected to be the array of device tokens of all users in confirmedGuests.
However, when confirmedGuests object is created, the function returns an error.
Below is my cloud function
exports.sendNotification = functions.database
.ref('/feed/{pushId}/confirmedGuests')
.onCreate((snapshot, context) => {
const pushId = context.params.pushId;
if (!pushId) {
return console.log('missing mandatory params for sending push.')
}
let allDeviceTokens = []
let guestIds = []
const payload = {
notification: {
title: 'Your request has been confirmed!',
body: `Tap to open`
},
data: {
taskId: pushId,
notifType: 'OPEN_DETAILS', // To tell the app what kind of notification this is.
}
};
let confGuestsData = snapshot.val();
let confGuestItems = Object.keys(confGuestsData).map(function(key) {
return confGuestsData[key];
});
confGuestItems.map(guest => {
guestIds.push(guest.id)
})
for(let i=0; i<guestIds.length; i++){
let userId = guestIds[i]
admin.database().ref(`/users/${userId}/deviceTokens`).once('value', (tokenSnapshot) => {
let userData = tokenSnapshot.val();
let userItem = Object.keys(userData).map(function(key) {
return userData[key];
});
userItem.map(item => allDeviceTokens.push(item))
})
}
return admin.messaging().sendToDevice(allDeviceTokens, payload);
});
You're loading each user's device tokens from the realtime database with:
admin.database().ref(`/users/${userId}/deviceTokens`).once('value', (tokenSnapshot) => {
This load operation happens asynchronously. This means that by the time the admin.messaging().sendToDevice(allDeviceTokens, payload) calls runs, the tokens haven't been loaded yet.
To fix this you'll need to wait until all tokens have loaded, before calling sendToDevice(). The common approach for this is to use Promise.all()
let promises = [];
for(let i=0; i<guestIds.length; i++){
let userId = guestIds[i]
let promise = admin.database().ref(`/users/${userId}/deviceTokens`).once('value', (tokenSnapshot) => {
let userData = tokenSnapshot.val();
let userItem = Object.keys(userData).map(function(key) {
return userData[key];
});
userItem.map(item => allDeviceTokens.push(item))
return true;
})
promises.push(promise);
}
return Promise.all(promises).then(() => {
return admin.messaging().sendToDevice(allDeviceTokens, payload);
})

push firebase data inside an array + js + vuejs

I have in firebase firestore a Collection named users created with docs of unique id.
Now I would like to push them in an Array.
(In the usersCollection there are 3 users stored with the currentUser.uid)
Example:
fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
querySnapshot.forEach((doc) => {
const userName = doc.data().name
this.markerMy = { name: userName }
})
// push userName inside randomArray
const randomArray = []
randomArray.push(this.markerMy)
I only get it so that I can push one user inside the Array, but not more.
You should declare randomArray before fb.usersCollection and call the push operation inside the callback as follows :
const randomArray = []
fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
querySnapshot.forEach((doc) => {
const userName = doc.data().name
this.markerMy = {
name: userName
}
randomArray.push(this.markerMy)
})
});

Categories