Update an existing row in DB in new data

How can i update new date in an existing row without loosing the old data?
I guess i need to extract the row existing data (using Get() ) and then update it with the new data but i’m not sure how to do it without loosing the existing data

Hi Oren,

indeed you are right, you read the data from your collection first to get a JSON item like this:

{
  "_id": "00001",
  "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
  "_createdDate": "2017-05-24T12:33:18.938Z",
  "_updatedDate": "2017-05-24T12:33:18.938Z",
  "title": "Mr.",
  "first_name": "John",
  "last_name": "Doe"
}

the you update the fields you need and continue according to the example here:
http://www.wix.com/code/reference/wix-data.html#update

Shlomi

Ok, but i don’t understand how to extract the JSON item and then update it with new data and then send the JSON again to update the collection. Do you have any example for it?

Hi Oren,

good point, we actually do not have such a working example in documentation so i’ve asked our documentation team to add such.
in the mean while if you have some of the code written, please share the url of the page you are building so we will be able to help

Shlomi

Yes Sure.
This is the issue:

  1. I try to create a cascade form - The user will fill a few fields in each slide of the slideShow.
  2. All the slideShow data need to be save under the same row in the DB (Webselenese)
  3. My problem is on function checkListAndUpdate. I can extract the correct row from the DB but i don’t know how to add extra data without fill all the old data again.

https://www.wixmeup.com/one-time-deal-test5

Oren

Hi Oren,

please note in the update documentation the following section:
http://www.wix.com/code/reference/wix-data.html#update

If an item in the collection has that _idvalue, update replaced the item’s property values with the ones in the specified item. If the existing item had properties with values and those properties were not included in the supplied item, the values in those properties are lost. The item’s _updatedDate property is also updated to the current date.

so you will need to copy all the fields from the item you read in your db, just like you do with the _id, you need to do the same with all the rest of the fields

let toUpdate = {
 "_id": exists.items[0]._id ,
 "email" : emailSession,
 "wixTemplate": wixDesign
 };

Shlomi

Ok, i understand but it seem like an inefficient way:(

If you have a dataset on the page, you can try just using setFieldValue. This doesn’t require you to query out the item, change it then update it.

But how do i know if i update the correct row each time i’m using setFieldValue?

if you’re purely using code, you’ll have use Shlomi’s way. If you’re using datasets that you paste on the page, you can set the index to a specific index.

Can your elaborate on how you’re currently doing this process. Code, etc?

Oren,

Here’s a very simple example of “Shlomi’s way” - get() a record, make a change, and then update():

wixData.get("Stuff", <record id>)
.then((toUpdate) => {
   toUpdate.fld = "x";
   wixData.update("Stuff", toUpdate)
   .then((result) => {
      // updated object returned so let's take a look
      console.log(result);
   })
   .catch((err) => {
      let errorMsg = err;
   });
})
.catch((err) => {
   let errorMsg = err;
});

I hope this helps.

Yisrael

Dear Ysrael, Thank You very much!
Can You help me find answer one one important question.

I try solve one problem during week or more and cannot.
I write code use Your example and it work
but when I try update my database in FOR loop
with big amount of Rows - some of this rows lost…

If I update 10 rows - It’s working
If I manually change index in FOR statements (0-10, next 11-20, next 21- 30…)
Finally Database records updated completely, but if I run "FOR from 0 to 90 -
30% of records (rows) not updated… lost (stay in previous data state)

I guess that problem with time responding database and I errors happened when I try to write until database ready, but I cannot find answer nowhere… How be sure that database ready again

AND

I read Your another advice from June 2019
And I understand в that mistakes happened because Database not ready when I make to much requests
But

  1. How I put my ONCLICK Button external function inside Onready?
    It’s impossible

  2. When I put database Refresh inside ONREADY some Table wit my datasate Refreshed on screen. (this result - obviously)

I will be appreciate any help…
Thank You in advance.
Sincerely, Misha

Here code fragment…
in FOR loop i put index values 70 - 81
to show that with small amount of - update working.
If I put 0-91 - 30% of records - not updated


export function button3_click(event) {

let i = 0;
let recid = 1;

  wixData.query("dbBardSongs") 
       .eq("bardName", selectedKey) 
       .limit(130) 
       .find() 
       .then( (results1) => { 

for (i=90; i<98; i++) {
recid = results1.items[i]._id;
onerecord2(i, recid);
}

  }) 
  . **catch** ((err) => { 

//let errorMsg = err;
console.log("Error in Query = ");
console.log(err);
});
}

export function onerecord2 (index, recid) {
let i = index;
wixData.get(“dbBardSongs”, recid)
.then((toUpdate) => {
toUpdate.orderNumber = (i + 1);
wixData.update(“dbBardSongs”, toUpdate)
.then((result) => {
})
. catch ((err) => {
//let errorMsg = err;
console.log("Eror in Update = ");
console.log(err);
});
})
. catch ((err) => {
//let errorMsg = err;
console.log("Error with Get = ");
console.log(err);
});

}

Almost All Error I receive in CATCH “Error in Get” but No CODE of Error - Just word “Error”