Updating record data

Hello everyone, I’m having a problem in updating a record in the database where a person press the update button and it should update the record. However, all the methods that have been mentioned below does not update the record. What could be the error/issue?

Please find below the code for updating the record:

let  IDQuery = wixData.query("AttendeeDB").eq("attendeeId",userID);
let  userID = session.getItem('transID'); //Where result will be a string.

export function button1_click(event) {
 //Add your code for this event here:
 let toSave = {
 "title":       $w('#Title').value,
 "firstName":   $w('#FirstName').value,
 "lastName":    $w('#LastName').value,
 "phoneNumber": $w('#PhoneNumber').value,
 "occupation":  $w('#Occupation').value,
 "specialty":   $w('#Specialty').value,
 "email":       $w('#Email').value,
 "password":    $w('#Password1').value
  };

 //Method1:
  wixData.save("AttendeeDB", toSave).then((results) => 
  {
 let item = results; //see item below
  }).catch((err) => 
  {
 let errorMsg = err;
  });

 //Method2:
  wixData.query("AttendeeDB").eq("attendeeId",userID).find().then((results) => 
  {
 let item = results.items[0];
 let resultCount = results.totalCount;
      item.title = $w('#Title').value;
      item.firstName = $w('#FirstName').value;
      item.lastName = $w('#LastName').value;
      item.phoneNumber = $w('#PhoneNumber').value;
      item.occupation = $w('#Occupation').value;
      item.specialty = $w('#Specialty').value;
      item.email = $w('#Email').value;
      item.password = $w('#Password1').value;
      wixData.update("AttendeeDB", item);
  }).catch((err) => 
  {
 let errorMsg = err;
  });

 // Method 3:
  $w("#dataset1").setFilter(IDQuery).then(() => 
      {
        $w("#dataset1").setFieldValues({
 "title":       $w('#Title').value,
 "firstName":   $w('#FirstName').value,
 "lastName":    $w('#LastName').value,
 "phoneNumber": $w('#PhoneNumber').value,
 "occupation":  $w('#Occupation').value,
 "specialty":   $w('#Specialty').value,
 "email":       $w('#Email').value,
 "password":    $w('#Password1').value
          })
          }).catch((err) => 
  {
 let errorMsg = err;
  });
 
  $w("#text19").show();
  $w("#text19").text = "Information has been updated";
  setTimeout(wixLocation.to(`/mainpage`), 2000)
}

#updatedatabase #updaterecord

Also the problem is that in the database, it adds a new record instead of updating

You should be using wixData.update, something like this…

import wixData from 'wix-data';

// ...

let toUpdate = {
  "_id":          "00001",
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.update("myCollection", toUpdate);

@mikemoynihan99 I tried to use this code, however, it didn’t work.

@mikemoynihan99 Here is the code that I used:

let toUpdate = {
 "title":       $w('#Title').value,
 "firstName":   $w('#FirstName').value,
 "lastName":    $w('#LastName').value,
 "phoneNumber": $w('#PhoneNumber').value,
 "occupation":  $w('#Occupation').value,
 "specialty":   $w('#Specialty').value,
 "email":       $w('#Email').value,
 "password":    $w('#Password1').value
  };
wixData.update("#dataset1", toUpdate);
wixData.update("AttendeeDB", toUpdate);

@mohamed

You have left out the “_id” of the item in your collection that you want to update…

The update() function compares the _id property of the specified item with the _id property values of the items in the specified collection. If an item in the collection has that _id value, update replaced the item’s property values with the ones in the specified item.

@mikemoynihan99 Thanks, it worked. However, when I view the profile it also updates ID parameter and erases it which I don’t want it to. Then, I can’t view the candidate profile as I am using search code that uses ID and email in order to filter and view candidate profile.

@mohamed

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.

Further info here wix-data - Velo API Reference - Wix.com

@mikemoynihan99 Is there other way to update a record?