[Changed] wixData.update() is makng new item in DS why?


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
    .then((sres) => {
       if (sres.items.length === 0) {
            return "No Item Found";
            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
                    .contains("frameNo", search_frame_no)
                    .contains("frameNo", search_frame_no)
                    .contains("frameNo", search_frame_no)
                .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")
//  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) => {

I have checked the id, which update function gets is true
Thanks in advance.

You incorrectly have both await and .then() for the calls to wixData.query and wixData.update. I don’t know if that’s connected to your problem, but you will need to correct this first. You should only use one of them - whichever you prefer.

See the following for more information regarding coding with Promises:

Thanks for replying.

I have done the changes as per your suggestion. But the problem I raised is still not solved. wixData.update() is still making new items in dataset.


Sorry, my question has changed now-
No new item is formed, but it only updates those items which I specify and removes all the rest value in the row.

I got to know it from here -

Site Link - https://www.wix.com/corvid/reference/release-notes/release-notes#release-notes_release-notes_updated-bulkupdate-clarification

Now the problem is that I have 90 columns in my dataset and It is troublesome to specify all of them.

Please refer to the documentation of the update() API function. It states:

If the existing item had properties with values and those properties were not included in the specified item, the values in those properties are lost.

Yeah, I know that and that is what I mentioned earlier. But the fact is that it would become very difficult for me to specify each and every item to get updated as I have 90 columns in my dataset.

So I am asking that is there any other method to update my dataset without losing the items I won’t mention or we could do some changes in the wixData.update() function itself to get things sorted.

@rinshulgoel First get the item to be updated. Then update the fields in the returned item object and use that object in the update() function.

Thanks it helped, and I just get it checked​:sweat_smile::sweat_smile:

1 Like