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 = {
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...
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;
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;
} );
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 ?