Add Numbers to Data Collection Records

I’m trying to assign a numerical value to each record in a jobs database so that a particular job can be referenced by its job number. I’ve modified some code that I found which Yisrael had submitted to the Forum. Unfortunately, when I input some test jobs, all submitted records show a value of 0. If someone would take a look at my modified code and show me where I’ve erred, I would be greatly appreciative.

import wixData from “wix-data”; // you’ll need this too

export function Jobs_beforeInsert(item, context) {
return wixData.query(“Jobs”)
.limit(1)
.descending(‘reference’) // sorts the results in descending order by ‘reference’
.find()
.then((results) => {
if(results.length === 0) {
item.reference = 1; // start at 1 for an empty collection
}
else {
const lastItemInCollection = results.items[0]; // get item with last reference number
item.reference = lastItemInCollection.reference + 1; // increment last reference number
}
return item; // beforeInsert function returns the modified item (record) to be saved
});
}

Hi,
Not:

if(results.length === 0) {

But:

if(results.totalCount === 0) {

J. D., thank you for the prompt reply. Now, in my Jobs data collection I’m getting the following with each test record, “The value does not match the field type Number.” The field type is currently set as # Number. When I select the “Convert” option the value that appears for each record is 0.