Question:
I would like to copy multiple data extracted from a collection to another with a query (see below)
Product:
Wix Editor
What are you trying to achieve:
Hello everybody
I have a collection A - “MesReservations” - (which groups together my reservations with the desired services), and a collection “B” - " Detailscommandes" - in which I would like to insert only the services (extracted from the coll “A”")
I created a wixdata.query on a very specific value (reservation number of collection “A”), and then it copies all the services relating to this reservation in collection “B”).
Pb: all data collected is transferred to database “B” but in a single record.
here my code below :
What have you already tried:
$w(‘#Services’).onClick((event) => {
return wixData.query(‘MesReservations’)
.eq(“Numreservation”, $w(‘#NumReservation’).value)
.find()
.then((results) => {
console.log(results.length + " réservation(s) répertoriée(s)")
if (results.length > 0) {
var NomService= results.items.map(item => item.Nomservice).toString()
var TarifService= results.items.map(item => item.tarif).toString()
var QuantiteService= results.items.map(item => item.quantite).toString()
let toInsert = {
"Service":NomService,
"Tarif": TarifService,
"Quantite":QuantiteService,
};
wixData.bulkInsert("Detailscommandes", [toInsert])
.then((item) => {
console.log(item)
})
} else {
console.log("Pas de réservation correspondant à ce N°")
}
})
})
Is the issue that the data is being added to the collection as one entry rather than multiple?
This is due to the structure of the object you used in the argument for bulkInsert()
let toInsert = {
"Service":NomService,
"Tarif": TarifService,
"Quantite":QuantiteService,
};
wixData.bulkInsert("Detailscommandes", [toInsert])
.then((item) => {
console.log(item)
});
To insert multiple items you should format the 2nd parameter of bulkInsert() (currently [toInsert]
) to a array of objects.
Thanks for your answer.
Effectively, data is being added to the collection as one entry rather than multiple.
Sorry but i don’t understand " format the 2nd parameter of bulkInsert() (currently [toInsert]
) to a array of objects."
How do i do this?
hello,
I tried this one below, but all data in collection A - MesRéservations (even those i don’t need) is being added to the collection B - Detailscommandes as one entry rather than multiple**
//****** Here the new code : ********
return wixData.query(‘MesReservations’)
.eq(“Numreservation”, $w(‘#NumReservation’).value)
.find()
.then((results) => {
var NomService= results.items.map(item => item.Nomservice).toString()
var TarifService= results.items.map(item => item.tarif).toString()
var QuantiteService= results.items.map(item => item.quantite).toString()
if (results.items.length > 0) {
const AddData = results.items.map(item => ({ ...item, Service: NomService, Tarif:TarifService,Quantite:QuantiteService}));
wixData.bulkinsert("Detailscommandes", AddData );
} else {
console.log (“pas de correspondance”)
}
})
.catch((err) => {
console.log(err);
});
Can you export an example of the data you have in Collection A. (as a csv file)
We can import it to actually see better what your results should look like.
Especially how the services are constructed in the Collection.
Also may be an idea to create a dummy collection B and input manually a data example so we can see how you expect it to look. And also export that (as a csv file)
Another question, which may or may not be pertinent is. This looks like you are trying to create a reservation look up system and use the second collection to populate a repeater or something, if so does the collection B only ever contain one entry at any one time?
We then could possible test ourselves and offer more constructive advice. Without that we are guessing at the collection set up.
1 Like
Hi
thanks for your answer. i do this as soon as possible and come back to you.
1 Like
Hello Mark
I am attaching my 2 collections (A & B)
In my collection “A” I have all the fields. In my collection “B” I would like to recover only the fields “N° de commande”,“Date commande”,“Article”,“Quantité”,“Mail Client”.
The pb is the data is being added to the collection as one entry rather than multiple.
I am also attaching a screenshot of result “A” which does not suit me (data grouped in a single line) and result “B” which I would like to have: the detailed results line by line.
How to join you CSV files?
Here the code i use :
wixData.query('COLLECTION A')
.eq("mailClt", $w('#MailClt').value)
.find()
.then(async (results) => {
if (results.length > 0) {
console.log(results.length + "article(s) a intégrer dans COLLECTION B")
var Article = results.items.map(item => item.article).toString()
var Quantite = results.items.map(item => item.quantite).toString()
console.log(results.items[0]);
let toInsert = {
"NumCommande":$w('#NumCommande').value,
"article": Article,
"Quantite":Quantite,
"mailClient":$w('#MailClt').value,
"DateCommande":$w('#DateTXT').value
};
wixData.bulkInsert("COLLECTION B", [toInsert])
.then((item) => {
console.log(item)
})
} else {
console.log("Pas d'article a intégrer dans COLLECTION B")
}
})
To export as csv open the cms and go to the More Actions Menu.
There you will see Export to CSV.
For the B Coll.
Just create a dummy one and manual enter some details showing how you want it to be. Then export that. That way it will be more clear of how you want the end result.
You will then need to Zip the files. and post them here.
Ok for export the collection, but not possible to post them (csv or zip). only images possible
Ah did not realise zips were band.
I will DM you with an email address were you can send them in a https://wetransfer.com
P.s
I have working code but just want to make sure it does so with your data and how you want
Also what with these, how are you first getting these values into inputs.?
You do not include them in the find.
I’ve gone ahead and updated my code to match your last visual example. ( not seen any csv)
A couple of things to note about my code.
The collection name and ifield ids may need to be changed to match yours.
For the CMS dates I used a Date field with the Include Time option ticked
I have made an assumption that the data for an entry in Collection A may change.
i.e Quantity etc.
Therefor my code will try and update entries in Collection B.
If the Entry does not exist in Collection B, the code will then insert it.
The other important thing being done, is we are getting the entry ID for an entry in Collection A, and using that to assign the id in Collection B. This insures no duplication of entries.
The idea here, is when the code runs it will either update the entries in Collection B or add them and will not duplicate entries.
$w("#Services").onClick((event) => {
const MailCltRes = $w("#MailClt").value
return wixData.query("CollectionA")
.eq("mailClient", MailCltRes)
.find()
.then((results) => {
console.log(results.length + " réservation(s) répertoriée(s)")
console.log(results)
if (results.length > 0) {
results.items.map( item => {
let toInsert = {
"_id":item._id,
"Num De Commande": item.numDeCommande,
"Article":item.article,
"Quantite":item.quantite,
"MailClient":item.mailClient,
//"mailClient":$w('#MailClt').value,
"DateCommande":item.dateCommande
//"DateCommande":$w('#DateTXT').value
};
console.log(toInsert)
wixData.update("CollectionB", toInsert)
.then((item) => {
console.log("Item updated OK");
})
.catch((error) => {
console.error("Update failed, item may not exist. Attempting insert...");
// Attempt insert if update fails
wixData.bulkInsert("CollectionB", [toInsert])
.then((items) => {
console.log("Item inserted OK");
})
.catch((insertError) => {
console.error("Insert also failed:", insertError);
});
});
})
} else {
console.log("Pas de réservation correspondant à ce N°")
}
})
})
1 Like
Hello Mark,
Thank you very much for your work. This was exactly what I wanted to achieve. By the way, your idea of update entries in Collection B if already exists and If the Entry does not exist in Collection B, the code will then insert is very good. My tests are conclusive: everything is OK.
Thanks a lot and have a nice day
1 Like