I have a calculated field and I need to save it into a database.
The calculated field is working fine but the save button doesnt
The code:
//CALCULATED FIELD
export function calcularIMC_click_1(event, $w) {
let Talla = Number($w( “#talla” ).value);
let Peso = Number($w( “#peso” ).value);
$w( “#imc” ).value = (Peso/(Talla*Talla)).toString();
}
//SAVE BUTTON
export function salvar_click(event){
$w( “#dataset1” ).setFieldValue( “mdImc” , $w( “#imc” ).value);
}
It doesnt work. Can you help me?
Try this one…
exportfunction salvar_click(event){
console.log("Start-Saving-Process")
$w("#dataset1").setFieldValue("mdImc", $w("#imc").value);
$w("#dataset1").save();
}
Check also, if you get “Start-Saving-Process”-output in CONSOLE?
Thanks Dima!
Yes. In the console I get Start-Saving-Process! But still not saving the value field in the database
Your BUTTON seems to work, check other things like…
-Check —> DATABASE-PERMISSIONS ! (set to all permissions to test it)
-Check —> DATASET —> READ & WRITE ? (set your dataset to read & write)
-does it work in Preview-Mode?
-put the first item —> MANUALLY into your DATABASE (test it again)
If still not work, show the first lines of your databse & your current used code.
Now it saves it but always with the number “0” 
What are the results of your calculations?
exportfunction calcularIMC_click_1(event, $w) {
let Talla = Number($w("#talla").value);
console.log(Talla)
let Peso = Number($w("#peso").value);
console.log(Peso)
$w("#imc").value = (Peso/(Talla*Talla)).toString()
;}
Now it saves it but always with the number “0” 
This is not enough INPUT !
It calculates your weight/height to see if you are in obesity risk. So the results are for example 16.2 or 20.4
Check how your DATABASE-FIELDS are configured —> STRING ? NUMBER ? Something else? How are configured your INPUT-FIELDS ?
Convert Number into String or String into Number.
Something like this…
12.toString()
Number(“12”)
Still not working?
Then create a TEST-BUTTON with a TEST-INPUTFIELD.
Write a simple CODE —> When press on BUTTON —> SAVE to DATABASE.
If this work, then continue to search for your error from this point.
Do everything step by step.
The field in de database is a Number Field
The field in the form is a Number field with decimals.
export function bttImc_click(event, $w) {
let Talla = Number($w( “#talla” ).value);
let Peso = Number($w( “#peso” ).value);
$w( “#imc” ).value = (Peso/(Talla*Talla)).toString();
}
export function salvar_click(event){
console.log( "Start-Saving-Process" )
$w( "#dataset1" ).setFieldValue( "mdImc" , $w( "#imc" ).value);
$w( "#dataset1" ).save();
}
$w.onReady( () => {
$w("#dataset1").onReady( () => {
$w('#bttImc').onClick(()=>{
let Talla = Number($w("#talla").value);
let Peso = Number($w("#peso").value);
console.log(Talla)
console.log(typeof Talla)
console.log(Peso)
console.log(typeof Peso)
console.log($w("#imc").value)
console.log(typeof $w("#imc").value)
$w("#imc").value = (Peso/(Talla*Talla)).toString();
})
$w('#salvar').onClick(()=>{
console.log("Start-Saving-Process")
console.log($w("#imc").value)
console.log(typeof $w("#imc").value)
$w("#dataset1").setFieldValue("mdImc", $w("#imc").value);
$w("#dataset1").save();
});
});
});
1 Like
Do a little simple test…
$w.onReady( () => {
var RESULT
$w("#dataset1").onReady( () => {
$w('#bttImc').onClick(()=>{
let Talla = 2;
let Peso = 3;
console.log(Talla)
console.log(typeof Talla)
console.log(Peso)
console.log(typeof Peso)
console.log($w("#imc").value)
console.log(typeof $w("#imc").value)
RESULT = (Peso+Talla); //should be ---> 5 (Number)
console.log(typeof RESULT)
})
$w('#salvar').onClick(()=>{
console.log("Start-Saving-Process")
console.log($w("#imc").value)
console.log(typeof $w("#imc").value)
$w("#dataset1").setFieldValue("mdImc", RESULT);
$w("#dataset1").save();
});
});
});
Does it work ?
Update:
$w('#salvar').onClick(async()=>{
console.log("Start-Saving-Process")
console.log($w("#imc").value)
console.log(typeof $w("#imc").value)
await $w("#dataset1").setFieldValue("mdImc", RESULT);
$w("#dataset1").save();
});
YES! That way it saves “5” into the database!
This it what the console log shows:
1
number
2
number
string
number
Start-Saving-Process
string
@russian-dima Hi Dima!
YES! That way it saves “5” into the database!
This it what the console log shows:
1 number 2 number string number
Start-Saving-Process string
@danieltalero78
I hoped you would get your solution on your own. 
As you can see, there must be a problem with the right type of your variable.
Just do some BRAINSTORMING. Look what is different between your problematic situation and my little and very simple example, which worked for you.
Use more CONSOLE-LOGs, so you will be able to see more of the programmed process.
And do not forget, you can not calculate the mixed way with STRING s and NUMBER s. Try always to convert to NUMBER s first, before you do some CALCULATING . Then after CALCULATING , you can convert your RESULT back in what ever you want, for example back to STRING .
@russian-dima Thanks for your help
@danieltalero78
Give a feedback, if you could solve it by your own, or not.