Adding value to a list which already has that value
I'm face this problem, when I try to add a number to a list of numbers I can't add that number cause it already exist in that list its overwriting it. I tried all possible solution but couldn't come up with it
4 Replies
Hi @AshishRoshan, could you post your flow and where you're having the problem. It's not clear from your post.
this is the code for adding value to array but it checks if the value exist in the array or not if the value exist i dont add or it replaces it but i want it to still add it even if the value exist in the list
arrayUnion() checks uniqueness, hence your issue.
Perhaps try this:
export default async function addObjectToArray(collectionName, documentID, fieldKey, object, projectId) {
try {
const firestore = new Firestore({
projectId: projectId && projectId.length > 1 ? projectId.trim() : undefined
});
// Retrieve the current document
const docRef = firestore.collection(collectionName).doc(documentID);
const doc = await docRef.get();
if (!doc.exists) {
throw new Error('Document does not exist!');
}
// Get the current array and add the new object
let currentArray = doc.data()[fieldKey];
currentArray.push(object); // Add the object to the array
// Update the document with the new array
await docRef.update({
[fieldKey]: currentArray
}, { merge: true });
return { success: true };
} catch (e) {
return e;
}
}
no still not working