wixData.get() not working.

Hi, I’m trying to update the values in the collection column. I need update more than one results. I’ve tried the following code.

export function button8_click(event) {
 wixData.query("kisaandiary")
.eq("_owner", wixUsers.currentUser.id)
.find()
.then(r => {
    console.log(r.items);
 for (var index = 0; index < r.items.length ; index++){
 var aee = r.items[index]._id;
        console.log(aee);
        wixData.get("kisaandiary", aee)
                            console.log("GET PASSED")
                            console.log(aee)
                            .then( (results) => {
                                console.log("Going to Update"+results)
    results.kdtransfirm = $w('#input1').value; // updated last name
    wixData.update("kisaandiary", results.kdtransfirm);
 
  } )
}
})
}

OUTPUT
List of items from first query successful. For loop providing id’s correctly. Program terminates at .then of wixData.get() function
Cannot read property ‘then’ of undefined

I tried to update with query but that need to update all the fields. I just need to update only kdtransfirm field of the populated results. Please help me asap and TIA.

@wix-expert-velo-cert The reason for the error is that the .then function needs to be immediately after the wixData.get. You have a couple console.log calls in between.

However, it doesn’t look like you need to " get " the information because you already have it - from the query above. r.items[index] would be the same as the get result. It’s the whole collection record.

The update function requires that the object that you’re updating it with have all the values for all the fields. If you try to update only one field, you’ll lose the previous values of the other fields in the collection record.

Lastly, the good news is that you can use bulkUpdate in this case. Let the whole kdtransfirm assignment loop run and do the bulkUpdate afterwards using the modified query result array. That way, it will be quicker since you won’t be making so many calls to the data server.

 wixData.query("kisaandiary")
.eq("_owner", wixUsers.currentUser.id) 
.find().then(r=>{
   for(var index =0; index < r.items.length ; index++){
      r.items[index].kdtransfirm =$w('#input1').value;// updated last name
   }
   wixData.bulkUpdate("kisaandiary", r.items)
   .then((bulkResult) => {
      console.log(bulkResult);
   })
})

Hi, Thanks for the answer. I tried to use bulk update but unables because of array variable. It will be best if you can explain me about “r.items”. How this update only the required term?

I know I have results but I used get() to update only individual field which is possible only with get & update combination, according to what I know. I added the console.log() to check the function break. But this doesn’t have any effect on then. The issue is with the .then variable “resutls”. I’ve to make this empty. Also, I’ve update the my initial code to the following to make it working: -

export function button8_click(event) {
wixData.query("kisaandiary")
.eq("_owner", wixUsers.currentUser.id)
.find()
.then(r => {
    console.log(r.items);
 for (var index = 0; index < r.items.length ; index++){
 var aee = r.items[index]._id;
        console.log(aee);
 
        wixData.get("kisaandiary", aee)
            .then( results => {
                console.log(results);
                results.kdtransfirm = "";
                                console.log("Going to Update"+results)
    results.kdtransfirm = $w('#input1').value; // updated last name
    wixData.update("kisaandiary", results);
  } )
  .catch( (err) => {
 let errorMsg = err;
    console.log(errorMsg);
  } );
    }
//}
})
}

Even, I also know this is lengthy & time taking code that’s why I’m using your code :). I just put the correct code to make a choice for others according to the conditions.

Thanks for your valuable time for the detailed answer.

@wix-expert-velo-cert After the for loop runs, r.items is simply an array result of the above query with the kdtransfirm field updated for each of the items.

To state it another way, it’s not possible to do a wixData.update with an object that includes only an ID and the value of one field (kdtransfirm) and not have it remove the data in the other fields of the record. That is the way the function operates: It expects that when you update the record, you specify the values for all the fields that have data. That’s why the strategy of altering the one field in the result array that the original query produces, and then writing that back with a bulkUpdate, is a sensible choice. It will maintain the data of those other fields doing it that way.

I suggest that you put a console.log(r.items) after the for loop, and then examine the array. It’s important to understand what you’re dealing with.