Session data not inserting into CMS database

Question:
Hi, I am stuck, and I hope the answer is simple. I am reading a database value called the applicationID from a database table as a number. I increment that number as this will be a form application ID. I store that data into a session (so it can be retrieve across multiple web pages). I populate that number into an input number box. That number box is connected to the CMS database. when I send the data to the CMS database everything works except the number.

I am thinking…
since the session data number is set in the inputbox.value for some reason when I validate the data using Captcha that data isn’t being picked up

Product:
Wix Studio Editor with Dev Mode enabled

What are you trying to achieve:
Taking the session number after i increment it and use that as an application ID and upload that into the database as a primary ID key for linking all new applicant data across multiple database tables.

What have you already tried:
I have tried using text fields, input text, input number, database type number, database type text, I have tried setting the input box to valid… still nothing??? I have validated that the database permissions are set to “anyone can view content” and “anyone can add content.” What I noticed is if I manually enter a number replacing the applicationID, that number will go into the database, but not the automated session number that has been set as the value of the input box.

Additional information:
Here is my front end code, I am focused on uniID, it all populates fine. but the data isn’t going into the database. Is session data a special type (other than string or integer)?
Any help greatly appreciated, I am running out of ideas.

site: www.PrivateTacticalArmory.com
Page: Tb

import {session} from ‘wix-storage-frontend’;
import wixData from ‘wix-data’;
import {process} from ‘backend/submitT1.jsw’;
import wixLocation from ‘wix-location’;

//Variables
const collectionAppID = “AppID”;
const appIDKey = “bc5e76e2-6b5e-4099-8392-9ede9be72d34”;
let appNumberInc = 3;
var newAppID = 0;

$w.onReady(function () {
wixData.get(collectionAppID, appIDKey)
.then((item) => {

//Increment the starting applicaiton number
	let newAppID = (item.appId + appNumberInc);

//Get new applicant number
	$w("#uniIDText").value = newAppID;

//Set session key and value of the new applicantID
	session.setItem ("AppKey",newAppID);

//Get session value using key
	$w('#uniID2Text').value = session.getItem("AppKey");
	$w('#uniID').value = session.getItem("AppKey");

//Set uniID to validated
	$w('#uniID').valid;

})

//Captcha on timeout
	$w('#captcha1').onTimeout( () =>{
	$w('#submitbtn').disable();
})

//Captcha on error
	$w('#captcha1').onError( () =>{
	$w('#submitbtn').disable();
})

//Captcha is verified
	$w('#captcha1').onVerified( () =>{
	$w('#submitbtn').enable();
})

});

//Submit button event validations
export function submitbtn_click(event) {
if($w(‘#firstName’).valid
&& $w(‘#lastName’).valid
&& $w(‘#uniID’).value){
//$w(‘#errorText’).hide();

	//Create an object 
	let objTb = {
		//Token
		token: $w('#captcha1').token,
		//nested wix-data obj
		wix_data_obj:{
			firstName: $w('#firstName').value,
			lastName: $w('#lastName').value,
			uniID: $w('#uniID').value
		}
	};

	//Send object to the backend & receive
	process(objTb)
	.then( (res) =>{
		if(res.success){
			//wixLocation.to(`${wixLocation.url}`); //refresh page if sucess
		}
	});
}
else {
	$w('#errorText').show();
	$w('#errorText').text = 'Incomplete entries, please correct.';
	$w('#submitbtn').disable();
	$w('#captcha1').reset();
}	

}

I corrected this error and changed it to valid
&& $w(‘#uniID’).valid

Still not working. :frowning:

So I did identify this issue resides around a focus event. This is kind of a weak work around but I could set the objects at the load event as .focus(). Then when the user clicks or does something else the number is picked up. I am also testing with have multiple .focus() and trying .blur() events.

//Get session value using key
	$w('#uniID2Text').value = session.getItem("AppKey");
	$w('#uniID').value = session.getItem("AppKey");
	$w('#uniID').focus();  //Added this and number did go into CMS database!
	
//Set uniID to validated
	$w('#uniID').valid;

This is a poorly constructed workaround.
It leaves the site open to end-users making changes to the number.
It does not work if the object is read only or hidden.

Wix dev team what is the correct solution for inserting data into the CMS database, without end-user focus types of interact?ion? Is there a way to programatically push this data as validated under a submit button.

Any help greatly appreciated. Thanks. :slight_smile:

From what I can see, the uniID is something that I believe is read-only and the user cannot change it right?

If that’s the case, then you can use a cosnt and store the uniID inside of the variable, and then display it on a text label instead of a read only input box. And then while submitting the data, submit the variable instead of the input box value. This should help resolve your issue.

Hi Pratham, I hope your day is going well. :slight_smile:
Well I have tried what you suggested and I must be misunderstanding something, I get this error:
TypeError: (0 , submitT1.process) is not a function (not sure why I get this error but it inserts data)
Tb
Line 78

Error: WDE0007: Invalid update. Updated object must have a string _id property. (I get this error everytime I try to insert the uniID).

Do you have a code example, I am not understanding the string_id error, as the object is a variable string. Thank you again for your help.

Updated Front End Code
import { session } from ‘wix-storage-frontend’;
import wixData from ‘wix-data’;
import { process } from ‘backend/submitT1.jsw’;
import wixLocation from ‘wix-location’;

//Variables
const collectionAppID = “AppID”;
const appIDKey = “bc5e76e2-6b5e-4099-8392-9ede9be72d34”;
let appNumberInc = 3;
var newAppID = 0;
const uniID = 0;

$w.onReady(function () {
wixData.get(collectionAppID, appIDKey) //read base number from CMS
.then((item) => {

        //Increment the starting applicaiton number
        let newAppID = (item.appId + appNumberInc); //Increment by 3

        //Set session key with incremented value of the new applicantID
        session.setItem("AppKey", newAppID);

        //Get session value using key
        $w('#uniID2Text').value = session.getItem("AppKey"); //Text display
        const uniID = session.getItem("AppKey").toString(); //Set to a variable
    })

//Captcha on timeout
$w('#captcha1').onTimeout(() => {
    $w('#submitbtn').disable();
})

//Captcha on error
$w('#captcha1').onError(() => {
    $w('#submitbtn').disable();
})

//Captcha is verified
$w('#captcha1').onVerified(() => {
    $w('#submitbtn').enable();
})

});

//Submit button event validations
export function submitbtn_click(event) {

//Update AppID DB value
let updateKey = {
    "AppID": "$w('#uniID2Text').value"
}
//Send to database
wixData.update(collectionAppID, updateKey)

    .then((results) => {
        console.log(results); //see item below
    })
    .catch((err) => {
        console.log(err);
    });

//Validate and Send data to T1 DB
if ($w('#firstName').valid &&
    $w('#lastName').valid) {

    //Create an object 
    let objTb = {
        //Token
        token: $w('#captcha1').token,
        //nested wix-data obj
        wix_data_obj: {
            firstName: $w('#firstName').value,
            lastName: $w('#lastName').value,
            uniID: uniID //Send number to CMS as part of the object
        }
    };

    //Send object to the backend & receive
    process(objTb)  //line 78
        .then((res) => {
            if (res.success) {
                //wixLocation.to(`${wixLocation.url}`); //refresh page if sucess
            }
        });
} else {
    $w('#errorText').show();
    $w('#errorText').text = 'Incomplete entries, please correct.';
    $w('#submitbtn').disable();
    $w('#captcha1').reset();
}

}

Updated backend Code
import wixCaptchaBackend from ‘wix-captcha-backend’;
import wixData from ‘wix-data’;

export function processT1(objTb) {
// Authorize token and insert data
return wixCaptchaBackend.authorize(objTb)
.then(() => {
return wixData.insert(“T1”, objTb.wix_data_obj)
.then(() => ({ success: true }))
.catch((error) => ({ success: false }));
})
.catch((error) => ({ success: false }));
}

THIS THEAD IS DEAD!
Starting a new one with simpler code example.

I was a bit caught up with something so wasn’t available.

I have replied to your new post, check it out.

Hey, I’m a bit late to the party, but I ran into the same problem when I first tried to do this. If you look at the documentation it says:
“If an element is connected to a dataset, setting the element’s value in code does not set the value of the connected field in the dataset. That means if you use the dataset to perform a submit, the value changed in code is not reflected in the submitted item.”
(https://www.wix.com/velo/reference/$w/textinput/value)

What you need to do instead is use setFieldValue() to change the value in the dataset