Multiple different queries in http-functions.js

Hi

I hava a http-functions.js at Backend which sends information to a url, and it works fine.

I need to get information from 2 different Databases.

At the main function I do the first query, and inside of it I call function2. At function2 there is a new query of another database.

The second query doesn’t work at all. What an I doing wrong?

export function get_myFunction(request) {
let msg = {
“headers”: {
“Content-Type”: “application/json”
}

};

return wixData.query(“myDB”)
.eq("",)
.find()
.then( (results) => {
if (results.items.length > 0) {

let data = makeData1(item);
msg.body = {
“Msg”: data
};
return ok (msg);
etc…

function makeData1 (item){
let data= “sad”;
wixData.query(“Messages”)
.find ()
.then( (results) => {
if (results.items.length > 0) {
data = “1”;
} else {
data = “2”;
}
} )
. catch ( (error) => {
data = “3”;
} );
return data;
}

// I always get “sad” as data :frowning: please help

It’s not clear what your code is supposed to do, but one thing I see is that you pass item to the function makeData1() , but item is not defined anywhere, and item is not even used in the makeData1() function itself.

Please explain your code and what you are trying to do.

Hi. That’s because I’ve pasted only part of the code… it doesn’t matter. Everything works fine, except the second query. It doesn’t happen at all, the function returns “sad” like it ignores the query

OK, but without the code there’s no way to help.

Couldn´t the problem be that you are not waiting for the query to return its data? You set data to “sad”, then perform a query, but underneath, at the bottom, you immediately return “data”. I always make these things async and “await” for the query result before returning values.

Thank you both! Where and how should I use "await? here is the second function, where the query doesn’t work (I made few changes to make it clear):


function try1 (){
var data = “sad”;
wixData.query(“Messages”)
.find()
.then((results) =>{
if (results.items.length > 0) {
data = “11”;
} else {
data = “22”;
}
});
return data;
}

Do you want to make 2 independent queries at the same time or you want to make second query with the data received in first query?

//two independent querys
async function example1() {
  const resultOfTwoCalls = await Promise.all([
    wixData.query("myDB")
    .eq("***", "***")
    .find()
    .then(result => result.items),
    wixData.query("Messages")
    .eq('something', firstItem.something)
    .find()
    .then(res => res.items)
  ]);
  return {
    resultOfFirstCall: resultOfTwoCalls.resultOfFirstCall,
    resultOfSecondCall: resultOfTwoCalls.resultOfSecondCall
  }
}

//second query based on he first query
async function example2() {
  const resultOfFirstCall = await wixData.query("myDB")
    .eq("***", "***")
    .find()
    .then(result => result.items);
  const firstItem = resultOfFirstCall[0];
  const resultOfSecondCall = await wixData.query("Messages")
    .eq('something', firstItem.something)
    .find()
    .then(res => res.items);
  return {
    resultOfFirstCall,
    resultOfSecondCall
  };
}

Thank you so much!!!