Reputation: 1402
My goal is to update a specific document by adding +1 everytime is "buyed", This is how the data enters I can either use this:
const data = JSON.parse(window.localStorage.getItem("listaLibros"))
which comes like this in the console log:
or I can transform all that data into a single array (idk which way would be easier to achieve what I want to do which is why I mention both)
const allItems = [...data1, ...data2, ...data3]
and then it comes like this:
However the reason why I had them "separated" as 4,0,3 is because each array is from a DIFFERENT collection and this time I had none from the second collection which is why it brings 0.
Now the idea is that for every BOOK it adds a +1 as "ordered" in their respective collection each book has his own uid which in this case is added on the document as id one of the examples in the picture is PE-13620 for example.
how can I compare my array information which the collections I think the best approach would be using the one with the separate arrays that way I can divide it by collection
let data1 = data[0] //Collection A
let data2 = data[1] //Collection B
let data3 = data[2] //Collection C
I know you can use .where('id', 'array-contains', data[0].id) I think ? but how do I use this for updating each one matched ? and to add the +1 would it be as simple as doing something like this ?
.update({
ordered: ++1
})
Update 1 Image showing how the user select each piece of data from each collection before "paying"
Collection B is not always empty, but for this example it is.
Update 2
So when you are in the "pay" screen the 3 arrays from before merge into 1 (as I mention before... const allItems = [...data1, ...data2, ...data3])
and when the person is gonna press Pay I would need to know how to use the arrays data1, data2 and data3 to check into each book and update the collection and add a +1 in "ordered". So far I do a new document to store all this into an order but I haven't figure out how to update that the book has +1 in "ordered"
so for the order I do the following:
const docRef = db.collection('usuarios').doc(user.uid).collection('pedidos').doc(docId)
docRef.get().then((doc) => {
docRef.set({
acudiente: user.displayName,
email: user.email,
phone: phone,
nombre: nombre,
grado: grado,
total: totalPrice,
totalPagado: 0,
escuela: escuela,
metodoDePago: "ACH",
banco: "Global Bank",
estadoDePago: "Pendiente",
fecha: formattedDate,
id: docId,
data: allItems
}).then((r) => {
history.push("/Inicio");
})
})
//I was thinking here add a query or something (for each one) but didn't
//work it brings data1.id as undentified
const librosRefNuevos = db.collection('libros');
const queryRefNuevos = librosRefNuevos.where('id', 'array-contains', data1.id);
However if I do data1[0].id it does bring back the first id of the array so maybe a for each but idk how to do a for each with where... maybe I'm complicating myself that's why I'm here lol
I hope this was more descriptive let me know if require anything else
This is how it looks in the firebase
FIXED
const ref1 = db.collection('libros');
const increment = firebase.firestore.FieldValue.increment(1);
for (let i = 0; i < data1.length; i++){
ref1.doc(data1[i].id).update({
ordenado: increment
})
}
const ref2 = db.collection('libros_Opcionales');
for (let i = 0; i < data2.length; i++){
ref2.doc(data2[i].id).update({
ordenado: increment
})
}
const ref3 = db.collection('productos_AIB');
for (let i = 0; i < data3.length; i++){
ref3.doc(data3[i].id).update({
ordenado: increment
})
}
Upvotes: 0
Views: 346
Reputation: 1402
Based on the other answer I manage to make an answer as well that works for me
const ref1 = db.collection('libros');
const increment = firebase.firestore.FieldValue.increment(1);
for (let i = 0; i < data1.length; i++){
ref1.doc(data1[i].id).update({
ordenado: increment
})
}
const ref2 = db.collection('libros_Opcionales');
for (let i = 0; i < data2.length; i++){
ref2.doc(data2[i].id).update({
ordenado: increment
})
}
const ref3 = db.collection('productos_AIB');
for (let i = 0; i < data3.length; i++){
ref3.doc(data3[i].id).update({
ordenado: increment
})
}
Upvotes: 0
Reputation: 161
I would simply iterate through each data
array and check for the existence of a corresponding document in each collection before updating the field.
const data = JSON.parse(window.localStorage.getItem("listaLibros"));
for (int i = 0; i < 3; i++) {
for (const item of data[i]) {
// Check libros
const libro = await db.doc(`libros/{item.id}`).get();
if (libro.exists) {
await db.doc(`libros/{item.id}`).update({
ordered: libro.get('ordered') + 1
})
}
// Check libros_Opcionales
const libroOpcional = await db.doc(`libros_Opcionales/{item.id}`).get();
if (libroOpcional.exists) {
await db.doc(`libros_Opcionales/{item.id}`).update({
ordered: libroOpcional.get('ordered') + 1
})
}
// Check productos_AIB
const producto = await db.doc(`productos_AIB/{item.id}`).get();
if (producto.exists) {
await db.doc(`productos_AIB/{item.id}`).update({
ordered: producto.get('ordered') + 1
})
}
}
}
Upvotes: 1