I am creating a way to log information about car setups into a database and want the last recorded entry to prefill the form fields. For example 1 form field will ask for tire pressure so if the last pressure was 28 it will stay 28 until i submit a new entry.
Something similar to this but not sure how to tell it what items it can/should get.
David, you have to determine the total number or records in the target table so that you will know the index number of the last record. This code can do that.
$w.inReady(function () {
let lastPressure = 0; // if you need visibility to this variable, move it outside of the function.
wixData.query(“myCollection”)
.find()
.then( (results) => {
resultSet = results.items.length;
if (resultSet === 0){// The table is empty. Do what needs to be done like set the last pressure to a reasonable value
lastPressure = 28;
} else {
let recCount = results.items.length - 1; //get the total number of records in the resultSet and subtract 1 since the index is numbered beginning with zero
lastPressure = results.items[recCount].tirePressure; //get the value in the tirePressure field
}
})
console.log(lastPressure);
})
I personally use this code in my application in order to generate a customer number. Hopefully it will work for you.
The code below may work as well, but you still need to get the desired field value which is why I opt for a query against the actual table (or as Wix calls it a collection) since the #dataset might be filtered.
count = $w(" #dataset1 ").getTotalCount();