Update records using wixData.Update() creates a new record in the same collection

Hey everyone,
Thanks in advance for your time.

Im having a problem with my project, I’ve been looking in other threads but nothing seems to work with me. I have 8 columns in my Database/Collection but i only want to Update 7 of them. But when i click on the update button, it creates a new record with the values from the fields I wrote.

This is my current code:

import wixData from ‘wix-data’ ;

let nome = $w( “#text53” );

console.log(nome)
wixData.query( “jobs” )
.eq( “title” , nome)
.find()
.then((results) => {
if (results.items.length > 0 ) {
results.item[ 0 ].description = $w( “#input8” ).value;
results.item[ 0 ].jobType = $w( “#input5” ).value;
results.item[ 0 ].ambicoes = $w( “#input2” ).value;
results.item[ 0 ].projeto = $w( “#input6” ).value;
results.item[ 0 ].departamento = $w( “#input7” ).value;
results.item[ 0 ].responsabilidades = $w( “#input9” ).value;
wixData.update( “jobs” , results.items [ 0 ]);

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

let errorMsg = err;
});

Any help and suggestion is apreciated,

Thanks in advance

Hello Diego,

try this one, i think taht should work…

function UPDATE_JUST_ONE_ColumnItem  (parameter) {
 let toUpdateRowData = "title3";
    console.log(toUpdateRowData)

    wixData.query("DATABASE1")
    .eq("title", toUpdateRowData)
    .find()
    .then((results) => {
 if (results.items.length > 0) {
 
 let item = results.items[0];
            item.reference2 = "Smith";
            wixData.update("DATABASE1", item);
        }
    })
}

Just optimize it to your collection-data.

Please check the Wix API Reference as it would tell you there that you need to include all fields and not just a select few of them.
https://www.wix.com/corvid/reference/wix-data.html#update

update( )
Updates an item in a collection.

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. 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. The item’s _updatedDate property is also updated to the current date.

,

The last two examples will be of best use to you here.

Examples

Update an item with a specified ID in a collection

import wixData from 'wix-data';

// ...

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

wixData.update("myCollection", toUpdate)
  .then( (results) => {
		let item = results; //see item below
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );

/*  item is:
 *
 *  {
 *    "_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"
 *  }
 */

Update an item in a collection using data options

import wixData from 'wix-data';

// ...

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

let options = {
  "suppressAuth": true,
  "suppressHooks": true
};

wixData.update("myCollection", toUpdate, options)
  .then( (results) => {
		let item = results; //see item below
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );

/*  item is:
 *
 *  {
 *    "_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"
 *  }
 */


Get an item in a collection and update it
This example demonstrates the get() function followed by the update() function. When updating an item in a collection, all existing item properties must be passed to the update() function. If only the changed property is passed, the values of the other item properties will be lost. To ensure all item properties are included in the update, perform a get() on the item, change a property, and then update() the item.

import wixData from 'wix-data';

/*  existing item:
 *
 *  {
 *    "_id": "00001",
 *    "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Doe"
 *  }
 *
 */

wixData.get("myCollection", "00001")
  .then( (item) => {
    item.last_name = "Smith"; // updated last name
    wixData.update("myCollection", item);
  } )
  .catch( (err) => {
    let errorMsg = err;
  } );

/*  updated item:
 *
 *  {
 *    "_id": "00001",
 *	  "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Smith"
 *  }
 *
 */

Query an item in a collection and update it
This example demonstrates the query() function followed by the update() function. When updating an item in a collection, all existing item properties must be passed to the update() function. If only the changed property is passed, the values of the other item properties will be lost. To ensure all item properties are included in the update, perform a query() on the item, change a property, and then update() the item.

import wixData from 'wix-data';

/*  existing item:
 *
 *  {
 *    "_id": "00001",
 *    "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Doe"
 *  }
 *
 */

 wixData.query("myCollection")
  .eq("first_name", "John")
  .eq("last_name", "Doe")
  .find()
    .then( (results) => {
      if(results.items.length > 0) {
        let item = results.items[0];
        item.last_name = "Smith"; // updated last name
        wixData.update("myCollection", item);
      } else {
        // handle case where no matching items found
      }
    } )
    .catch( (err) => {
      let errorMsg = err;
    } );

/*  updated item:
 *
 *  {
 *    "_id": "00001",
 *	  "title": "Mr.",
 *	  "first_name": "John",
 *    "last_name": "Smith"
 *  }
 *
 */

It is possilbe to update all values that matchs with a criteria?

Example:

Field 1: User
Field 2: Title
Field 3: Ingredient

Field 2 is currently: Carpaccio de salmón

and looks like that:

Then , the user wants to replace the Field 2… “Carpaccio de salmón” by any other name.

At this moment, I have this code, but is only updating the last row submited… and I need to update any cell with the name 1, which was submited by the user

export function receta_click ( event ) {

**let**  user  =  wixUsers . currentUser ; 
**let**  userId  =  user . id ; 
**let**  titulo  =  $w ( '#receta' ). value ; 
console . log  ( "titulo es "  +  titulo ) 
    $w ( "#receta" ). onChange ( ( event ) => {  
        **let**  nuevoTitulo  =  event . target . value ; 
        console . log  ( "nuevo titulo es "  +  nuevoTitulo ) 
        wixData . query ( "Recetas" ) 
        . eq ( "usuario" ,  userId ) 
        . eq ( "title" ,  titulo ) 
        . find () 
            . then ( ( results ) => { 
            **if** ( results . items . length  >  0 ) { 
                **let**  item  =  results . items [ 0 ]; 
                item . title  =  nuevoTitulo ; 
                console . log  ( "Titulo anterior encontrado" ) 
                wixData . update ( "Recetas" ,  item ); 
                console . log  ( "Titulo "  +  titulo  +  " actualizado por "  +  nuevoTitulo ) 
                }  
            })     
        }) 
    } 

Many thanks for your great support!

You are only getting the first item:

let item = results.items[0];

What you need to do is go through the array of returned results.items and create an array of items to update. You can then call the bulkUpdate() function with the array of items to update, and they will all be updated at together.

Next time, please add your own issue into a new post instead of bumping up an old post (which is about a different problem).