I need some help with query results.

I need to use result from query only ‘value’ field and use it to calculate with input value ‘input1’. how to do it.
import wixData from ‘wix-data’;
export function button1_click(event) {
const inID = $w(‘#idSe’).value;
const inValue = $w(‘#input1’).value;

let toInsert = { 

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

if (inValue > 0) { 
	const alert = $w('#errorText').hide(); 
	wixData.query("ChamokInventory") 
		.eq("code", inID) 
		.find() 
		.then((results) => { 
			console.log(results.items);  // it show all data. i need only 'value' field. 
                                                                      //and use it to calculate with input1. 
		}); 

	wixData.insert("ChamokInventory", toInsert) 
		.then((results) => { 
			let item = results; //see item below 
			const alertSuc = $w('#succMsg').show(); 
		}) 
		.catch((err) => { 
			let errorMsg = err; 
		}); 
} else { 
	const alert = $w('#errorText').show(); 
} 

}

Hay Shiro,

You can access only the value field using the .value property access.

for instance, the following will print only the values.

wixData.query("ChamokInventory")
    .eq("code", inID)
    .find()
    .then((results) => {
        let values = results.items.map(_ => _.value) 
        values.forEach(console.log);
    });

thank you for answer. Value is 30 but output is 30 0 30 how to fix it to 30 only

Hi Shiro,

If you expect to get a single value back as the result you can access it with the following line:

wixData.query("ChamokInventory")
  .eq("code", inID)
  .find()
  .then((results) => {
     let value = results.items[0].value;
     console.log(value); //prints the value of the first result
     ); 
 });

Thank you for help. but i have little problem when use query result and get input element.
My input value is texbox number type ‘inValue’ , but when get value element it convert to string.
And when i try to use direct input from query it not contain any data from query insert to DB.

import wixData from ‘wix-data’;
export function button1_click(event) {
let values;
const inID = $w(‘#idSe’).value;
const inValue = $w(‘#input1’).value; //My input value
wixData.query(“ChamokInventory”)
.eq(“code”, inID)
.find()
.then((results) => {
values = results.items[0].value; //save output to values variable
console.log(values); ;
});
//let sum = inValue+values; //sum before insert but values is convert to string how to fix it
let toInsert = {
“code”: inID,
“value”: values //try to add direct from query but ‘values’ is not contain any data
//“value”: sum
};