Wix Data on backend

This is what I have. The backend function is receiving the data properly but it is not performing the query. I am not getting any error or object . I’ve tried get also but it does work on the frontend but not backend.

Frontend

import {email} from 'backend/email.jsw';

export function click(event) {
email(
         $w("#input1").value)
         .then(function() {
            console.log("email was sent");
        }
     );

}

Backend

import wixData from 'wix-data';

export function email (data1) {

let userEmail = data1

  console.log(userEmail);                //Console log sucesfull 
   
wixData.query("Profile")
  .find()
  .then( (results) => {
 let firstItem = results.items[0]; console.log(firstItem);
  } )
  .catch( (err) => {
 let errorMsg = err; console.log(errorMsg);
  } );

}

Hi,

What do you expect the function ‘email’ to do ?
It doesn’t use the user email in its query and doesn’t seem to be doing anything with the result of the query apart from printing the first item (given that the query was correct).

Please elaborate or provide a link to your site so I may assist you further.

Thanks,

Itay Koren

Hi, I have a similar issue. I am writing backend code on my site. I want to get the count (actually other data but for now doing the count only).
I am writing this in https-functions.js

But it returns ERR_EMPTY_RESPONSE

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

// URL to call this HTTP function from your published site looks like:
// Premium site - https://mysite.com/_functions/example/multiply?leftOperand=3&rightOperand=4
// Free site - https://username.wixsite.com/mysite/_functions/example/multiply?leftOperand=3&rightOperand=4

// URL to test this HTTP function from your saved site looks like:
// Premium site - https://mysite.com/_functions-dev/example/multiply?leftOperand=3&rightOperand=4
// Free site - https://username.wixsite.com/mysite/_functions-dev/example/multiply?leftOperand=3&rightOperand=4

export function get_getsponsorimages(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};

// query a collection to find matching items
let sdrQuery = wixData.query(“SDRSponsors”);

sdrQuery.count()
.then( (num) => {
options.body = num;
return ok(options);
} )
. catch ( (error) => {
options.body = error.message;
return ok(options);
} );

You code will look like this, Just do something like let count = whatever.whatever and then return count in the object where I show you

// Backend code

export function getUser(id) {

ler userId = id

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

 // query a collection to find matching items
 
 return wixData.query("Users")
        .contains('userId', userId)
        .find()
        .then((results) => {
            userid=results.items[0]._id

////////////////////////// Here is where you choose what to return
            options.body = {
 "items": results.items
            };
//////////////////////////////////////////////////////////////////

 return (options.body.items);

 // no matching items found
            options.body = {
 "error": 'NOT FOUND'
            };
 return (options);
        })
 // something went wrong
        .catch((error) => {
            options.body = {
 "error": error
            };
 return (options);
        });}