Basic backend wix-data query problem

I read and applied all the related problems here but could not solve the basic wix data examples to work. I am on this issue for several days and I think I need a help now.

I have http-functions.js and DataService.jsw files and trying to create a an api get endpoint with those.


http-functions.js has this code:

import {ok, notFound, serverError} from ‘wix-http-functions’;
import wixData from ‘wix-data’;
import {get_result} from ‘backend/DataService’;

export function get_myFunction(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
let resultshere = get_result(“1”);
options.body = resultshere.items;
return ok(options);


DataService.jsw has this code:

import wixData from ‘wix-data’;
import {ok, notFound, serverError} from ‘wix-http-functions’;

export function get_result(requestInfo) {
return wixData.query(“Customers”)
.find()
.then(results => {
return results;
})

}


I have a collection cold Customers by this configuration i think,
https://www.watchcontact.com/_functions/myFunction/ + any string like
https://www.watchcontact.com/_functions/myFunction/0 should show the Customers on options.body. But it does not work.

By the way, all the collection permissions are set to anyone.

When i tested in more detail, I have concluded that, wixData.query is working on the frontside (when it is under onReady function). But in the backend files like http-functions.js or DataService.jsw, this mechanism does not work.

The link is the real link that I am working on it. I have to solve the issue immediately.

Have you read the API Reference for Wix HTTP Functions as you need to add code for each of those functions of ok, notFound and serverError.
https://www.wix.com/corvid/reference/wix-http-functions.html

Also, have you read this page too about it.
https://support.wix.com/en/article/corvid-exposing-a-site-api-with-http-functions

Yes, I have read those 2 documents. Actually my code is a version of the tutorial document’s code.

Anyone to help?

See the following example:

Expose and Access Site APIs

Use MyApi and MyApiClient to expose and access external APIs.

I have read your code and mine is absolutely the same.

Can you say me what is wrong with my code below. This is the code of whole http-functions.js


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

export function use_myFunction(request) {
wixData.query(“Customers”)
.find()
.then(resultshere => {

let options = {
headers: {
“Content-Type”: “application/json”
}
};

if (resultshere.items.length > 0) {
options.body = “AAAA”;
}
else
options.body = “BBBB”;
return ok(options);
})
}


I have collection called Customers and its permission are set to Anyone as you see below.

When I call https://www.watchcontact.com/_functions/myFunction it give an http-500 error.

I think the problem is caused by the wixdata.query. I can not reach my collection somehow.

Any help would be great.

Thank you.

It looks like that I have the same problem.
When I try to perform a query with any of our collections, I receive an error.
The error object is an empty ({}) object.

The problem raised, when I tried to query a custom access collection. I have changed it such that it is permitted it to anyone (just for the tests). I still receive the same error in the query. Also when I tried other permissions levels I receive the same problem.

I wonder did you find the problem in your code?
Maybe I have the same problem.

Thanks in advance.

Steps to debug:
1 - Query collection in a page code
wixData.query('collection).find().then(res => {console.log(‘Results’, res) })

2 - Put the working query in the HTTP js file, preview the site and open the endpoint GET from another browser tab using the DEV function path:
(Publish the site before preview)
/_functions-dev/
The preview console will show the log.

FYI:
_function-dev/getfunction => preview mode (show console.log),
_function/getfuntion => live

If no log, the browser is using OPTION to pre-flight and/or you need to reply (send a response) with “Accept”: “application/json” and some “Allow…” headers.

By security, the Browser allows request with no cors checking for application/x-www-form-urlencoded, multipart/form-data, text/plain “Content-Type”.

Read the Velo doc and the mozilla docs about HTTP cors and HTTP headers.

Please add your own issue into a new post instead of bumping up an old post. Explain what you are trying to do, what works, and what doesn’t. Also, add any code in a code block as stated in the Forum Guidelines .

This is an old post and is being closed.