Database not update new data.

I need to update DB by used ID “code” to query and then update only “value” field, but it not any update in DB when i try .save it always insert not update. anyone can help me fix it.

import wixData from ‘wix-data’;

export function button1_click(event) {
let values;
const inID = $w(‘#idSe’).value;
const inValue = parseInt($w(‘#input1’).value, 10);
wixData.query(“ChamokInventory”)
.eq(“code”, inID)
.find()
.then((results) => {
values = results.items[0].value;
let sum = inValue + values;
let newupdate = {

			"code": inID,              
			"value": sum, 
		}; 

		if (inValue > 0) { 
			const alert = $w('#errorText').hide(); 

		wixData.update("ChamokInventory", newupdate)    **//update only "value" field** 
				.then((results2) => { 
					let item2 = results2;                        
					const alertSuc = $w('#succMsg').show(); 
				}) 
				.catch((err) => { 
					let errorMsg = err; 
				}); 
		} else { 
			const alert = $w('#errorText').show(); 
		} 
		console.log(values);; 
	}); 
// 

}

when i try
let toUpdate = {
“code”: inID,
“value”: inValue,
“title”:“a”,
“unit”:“ab”,
“type”: “abc”
};

Are you runing the page in live or sandbox mode?

sandbox mode

Hi Shiro,

See here about update.

Note that the update() function updates according to database ID.

So in your case I would go for reading the existing value, then assign a new data to it and update in database:

// Read old one
values = results.items[0].value;
let sum = inValue + values;

//assign new data
values.code = inID;
values.value = sum;

//.... update here...

Liran.

Thank a lot Liran
I try it, but when preview mode has error
wixData.query(“ChamokInventory”)
.eq(“code”, inID)
.find()
.then((results) => {
values = results.items[0].value;
let sum = inValue + values;
values.code = inID;
values.value = sum;

		let toUpdate = { 

			"code": inID, 
			"title": sum, 
			
		}; 

Oh, I thought ‘values’ is an object. :slight_smile:

You should use:

values = results.items[0];

Instead of;

values = results.items[0].value;

Good luck,
Liran

sorry for late reply, when i try to test update all field in my DB it not work.
And if i need to update only one field can i use this code ?
let toUpdate = {

	"code": "test",   //text         
	"value": 999, //number    
}; 

this is my code

import wixData from ‘wix-data’;

export function button1_click(event) {

let toUpdate = { 

	"code": "test",   //text        primary key 
	"title": "a",   //text  
	"value": 999, //number    **Need Update only this field** 
	"unit":"b", //text 
	"type":"c" //text 
}; 

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

}

Hi,
You have to include all fields, but most important - the ID field. otherwise, the database won’t know which record to update.

I suggest reading it first using ‘wixData.query()’ and then updating the fields in code and calling ‘wixData.update()’.

Something like:

import wixData from ‘wix-data’;

export function button1_click(event) {
	let values;
	const inID = $w('#idSe').value;
	const inValue = parseInt($w('#input1').value, 10);
	wixData.query("ChamokInventory")
		.eq("code", inID)
		.find()
		.then((results) => {
			values = results.items[0]; //old record
			let sum = inValue + values.value;

			//Updating old record in code
			//it includes the _id field
			values.code = inID;
			values.value = sum;
			
			if (inValue > 0) {
				const alert = $w('#errorText').hide();

			//update only "value" field, but send all previous field as well
			wixData.update("ChamokInventory", values)   
					.then((results2) => {
						let item2 = results2;                       
						const alertSuc = $w('#succMsg').show();
					})
					.catch((err) => {
						let errorMsg = err;
					});
			} else {
				const alert = $w('#errorText').show();
			}
			console.log(values);;
		});
	//

}

Hope this helps,
Liran.

thank you very much Liran.
Now i found my miss.

Hi Liran,

The code you gave does work but not for all the rows.

This is what I coded:

export function updateEmail_click(event, $w) {
 let user = wixUsers.currentUser; 
 let userId = user.id; 
 let values;
 const inValue = ($w('#input1').value);
    wixData.query("Cart")
        .eq("userId", userId)
        .find()
        .then((results) => {
            values = results.items[0]; //old record
 let sum = inValue;

 //Updating old record in code
 //it includes the _id field
            values.userId = userId;
            values.clientEmail = sum;
 
 //update only "value" field, but send all previous field as well
            wixData.update("Cart", values)   
                    .then((results2) => {
 let item2 = results2;
                    });
        });
}

As you can see in the screenshot, I have 2 rows with the same userId
How do I go about updating all the rows with the same userId

I don’t understand how should i implement async on this code. Can you help please ?

Hey
If you want to loop through records and update all rows you will have to create a loop.

let items = results.items:
items.forEach((item) => {
// Here you do the update and then every result item you get in your query will be looped here
})