[Answered] Backend wixData not working?

A question mark, for it doesn’t seem to work but maybe it is. I have made a backend file which is fairly simple. It just needs to query a table and return the result. Similar code in the frontend works. According to the documentation wixData.query is usable in the backend.

import wixData from 'wix-data';

export function getOverviewPoints(strLanguage) {
	let oTrails = strLanguage;
	wixData.query("trail_translations")
		.find()
	  	.then((oTrails) => {		
			this.oTrails =  oTrails;
		})
		.catch((errTrails) => {
			this.oTrails = errTrails;
		});
		
	return oTrails;
}

Above my backend file. The function is called from the frontend and also passes the language. The result of the above code will be the parameter I passed. What I expect is to get the query result. I also replaced ‘this.oTrails = oTrails’ with ‘return oTrails’ but then it is just empty. Yes there is data in the table

Any ideas?

I more final situation the backend code would be something like this

import wixData from 'wix-data';

export function getOverviewPoints(strLanguage) {
	let oTrails = [];
	
	wixData.query("trail_translations")
		.find()
	  	.then((oTrails) => {		
			for (const oTrail in oTrails) {
				this.oTrails.push({
					"trail_code": oTrail.translation_id
				});
			}
		});
		
	return oTrails;
}

The respoonse will be a subset of columns of the initial query. In this case you would expect ‘[{trail_code:1},{trail_code:2}]’

So I got it working. My backend function is now

import wixData from 'wix-data';

export function getTrailMarkers(strLanguage) {
	//return oMarkers;
	return wixData.query("trails")
		.find()
	  	.then((resultTrails) => {
	  		const oTrails = resultTrails.items;
	  		let arrTrails = [];
	  		
	  		// Transform to required columns
	  		for (const iTrail in oTrails) {	  			
	  			if (strLanguage === "nl") {
		  			arrTrails.push({
		  				"_id": iTrail,
		  				"trail_lat": oTrails[iTrail].trail_lat,
		  				"trail_long": oTrails[iTrail].trail_long,
		  				"trail_title": oTrails[iTrail].trail_title
		  			});
	  			} else {
		  			arrTrails.push({
		  				"_id": iTrail,
		  				"trail_lat": oTrails[iTrail].trail_lat,
		  				"trail_long": oTrails[iTrail].trail_long,
		  				"trail_title": oTrails[iTrail].trail_title
		  			});	  				
	  			}
	  		}
	  		
			return arrTrails;
		});
}

So it now queries, and then I string all unwanted columns, and implement multilingual data. In this case I don’t have different columns yet for ‘nl’. The array is then passed to the front end and further processed.

Glad to see you figured it out. Let us know if you need more help.