Problems with Getting a query to work

So i have generated a public.js file
contents of said file
import wixData from ‘wix-data’;
export function problems (){
var routeproblems = “”;
wixData.query(“database”)
.ne(“Status”, “Running”)
.find()
.then( (results) => {
var i = 0 ;
do {
routeproblems = routeproblems + " " + results.Route[i];
i++;
}
while (i <= results.length);

});
return routeproblems;
}

So basically we have a database for bussing , If the status changes to anything but running i need to report it back to the site, So i wrote this in my new-file.js

on the dynamic page, i have
import {problems} from ‘public/new-file.js’ ;
export function table1_click(event, $w) {
//Add your code for this event here:
var pb = problems();
if (pb !== “”){
$w(“#text18”).html = “The Following Bussing routes have problems” + pb;
$w(“#text14”).collapse();
$w(“#text17”).expand();
}
if (pb === “”){
$w(“#text18”).html = “All Busses are running as scheduled”;
$w(“#text17”).collapse();
$w(“#text14”).expand();
}
}
Where text 17/14 are text that i have formatted to be something specific for if there is a problem or not, But when i click on the table. no matter what is set in my status table i get "All busses are running as scheduled.

Any thoughts?

Hey,
Have you tried console log the pb variable? Do you get the relevant results?
You can get more info about debugging your code here .

Tal.

Good Afternoon Tal!

So i tried to console log the pb variable but it didnt show anything,

Can you please send the site URL and the page name to which the code was added so that I can have a look?

Thanks,
Tal.

https://sperdwebsites.wixsite.com/sperd-home-page-rt/StPaulBussing

Sorry https://sperdwebsites.wixsite.com/sperd-home-page-rt/ page is StPaulBussing.

Hi Travis,
I’ve made some changes in your code and added Async and Await declaration

export async function table1_click(event, $w) {
	//Add your code for this event here: 
	var pb = await problems();
	console.log('pb', typeof pb);
	if (pb !== ""){
		$w("#text18").html = "The Following Bussing routes have problems" + pb;
		$w("#text14").collapse();
		$w("#text17").expand();
	}
	if (pb === ""){
	 $w("#text18").html = "All Busses are running as scheduled";
	 $w("#text17").collapse();
	 $w("#text14").expand();
}
	
}

And:

export async function problems() {
	let routeproblems = "";
	let results = await wixData.query("StPaulBussing")
		.ne("Status", "Running")
		.find();
	var items = results.items;

		var i = 0;
		do {
			routeproblems = routeproblems + " " + items.Route[i];
			i++;
		}
		while (i <= results.length);
	return routeproblems;
}

Good luck!
Roi

Wow Thanks Roi, So Close it now generates a line of text that says this The Following Bussing routes have problems[object Promise] instead of The Following Bussing Routes have problems TH9 for example

Also lol if all the busses are running it still gives me The Following Bussing routes have problems[object Promise] . So very close

So i changed my DB status field to a numeric valus, and assigned them all 1 and then changed my .ne to .ne(“status”, 1)
.then();

And it still returns with "The Following Bussing routes have problems [objectPromise]. even when everythign is 1 and shouldnt return a value.