“QUESTION HAS NOW CHANGED, SEE THE LATER REPLY”
This is my code file (It is a web module)
wixData.update() is in the last.
I have added comments to make this code sense faster-
import wixData from 'wix-data';
let counter = 0; // to check how many times update has run
export async function upload_service_record() {
counter = 0
let rejected_frame_no = [];
let search_frame_no = "";
await wixData.query("Try") // to get the a field item (here - frameNo) from collection
.limit(100)
.find()
.then((sres) => {
if (sres.items.length === 0) {
return "No Item Found";
}else{
for (var i = 0; i < sres.items.length; i++) {
search_frame_no = sres.items[i].frameNo;
let last_service_date = sres.items[i].lastServiceDealer; // for updation purpose only
// Now I search the frame no in my 3 collections that is why i used for loop
Promise.all([
wixData.query("SRFU18-19-20")
.contains("frameNo", search_frame_no)
.limit(5)
.find(),
wixData.query("SRFU15-16-17")
.contains("frameNo", search_frame_no)
.limit(5)
.find(),
wixData.query("SRFU12-13-14")
.contains("frameNo", search_frame_no)
.limit(5)
.find()
])
.then(async(mres) => {
let coll1 = mres[0].items;
let coll2 = mres[1].items;
let coll3 = mres[2].items;
// Now I check in which dataset item is present and then i call the update function
if(coll1.length === 1){
let id = mres[0].items[0]._id;
await upadte_dataset(last_service_date, id, "SRFU18-19-20")
}else if(coll2.length === 1){
let id = mres[1].items[0]._id;
await upadte_dataset(last_service_date, id, "SRFU15-16-17")
}else if(coll3.length === 1){
let id = mres[2].items[0]._id;
await upadte_dataset(last_service_date, id, "SRFU12-13-14")
}else{
rejected_frame_no.push(search_frame_no);
}
})
}
}
})
console.log(counter);
}
// this is my update function but it is making new item in the datset
async function upadte_dataset (last_service_date, id, collection) {
let next_follow_date = new Date(last_service_date.getDate + 30)
let toUpdate = {
"_id": id,
"lastServiceDealer": last_service_date,
"nextServiceDate": next_follow_date
};
await wixData.update(collection, toUpdate)
.then((res) => {
counter++;
})
}
I have checked the id, which update function gets is true
Thanks in advance.