problem returning database query results from backend 'get'

I need to make several queries to the backend to extract info from my database collections, but attempts to return either the result of the query or even an error on query returns a Server error: 500, which suggests a programming error:

I am trying to call return ok(response) or return badRequest(response) from the .then or .catch blocks of the query Promise.

Here is the very simple code. (Calling with the ‘query’ operation WORKS FINE!):

And PS: the table I’m querying has ‘Anyone’ permissions for reading.


import {ok, badRequest, forbidden} from ‘wix-http-functions’;
import wixData from ‘wix-data’;

export function get_tables(request) {
const response = {
“headers”: {
“Content-Type”: “application/json”
}
};

// Get operation from the request path 
const operation = request.path[0]; 
const tableName = request.query["tableName"]; 

// Perform the requested operation and return an OK response 
// with the answer in the response body 
switch (operation) { 
case 'query': 
	response.body = { 
		"table": tableName 
	}; 
	return ok(response); 
case 'action': 
	wixData.query(tableName) 
	.find() 
	.then( (results) => { 
		response.body = { 
			"table": tableName 
		}; 
		return ok(response); 
	} ) 
	.catch ( (err) => { 
		response.body = { 
			"error": err 
		} 
		return badRequest(response); 
	}) 
	break; 
default: 
	// If the requested operation was not found, return a Bad Request 
	// response with an error message in the response body 
	response.body = { 
		"error": "unknown operation" 
	}; 
	return badRequest(response); 
} 

}

I think this article should point you in the right direction, especially the section titled Returning in a .then().

1 Like

Hi, Sam.

Thanks for the quick response. I do know how to use promises and use them all the time in my front end code for lots of DB queries, insertions, and updates. In the front end I’ve pipelined filtering methods on the .then to achieve completion of all necessary processing w/out having to block.

My problem was on the backend, but after much experimentation w/the ideas in the article you recommended, I was able to get my code working on the backend, but I wonder if that is only because the backend blocks for returns via ok(), badRequest(), forbidden(). ?? Maybe? Excepting of course exceptions jump to the 500 Error return.

HOWEVER, when I’ve tried to use the same patterns in my front end code (I now find a need to block on a DB read before performing other work, I’m unable to actually block until the async-awaited code completes.

Perhaps you can see why that might be? The async/await function is as close at it gets to the code in the article. The calling code, not quite so.

======================================

export async function findName(name) {
let results;
try {
results = await wixData.query(name)
.find();
}
catch(error) {
results = "DB failure for table " + error;
}
return results;
// return results.items[0];
}

******* and the code that calls it, which does NOT block:

let returnStuff = null;
findName(‘Products3’)
.then( (results) => {
returnStuff = results.items;
} )
.catch ( (err) => {
console.error('findName ERROR: ’ + err);
})

I don’t use an await in the calling code, because I can’t see how my onReady function can be async. Guess I could be wrong there.