Can you provide some advice of what can cause I am not receiving the data when a promise is called from the frontend .
When I run it from the backend, I am having the right information in return.
So I added a step in the backend to insert those data in a collection in addition.
Even there, I do not see the data when called from the frontend.
Try the settimout() with no result.
Try async/ await with no result.
Thanks in advance
Hello, to help others understand your issue and offer guidance can you post the relevant code snippets you have tried in the FE/BE?
Here is
Backend Code sample, because it is a complex function:
export async function calculTarif(selChalet, selFrom, selTo, selFromNum, selToNum, isMember, isInviter, isVisiteur, selFamMbr, selFamInv, selFamVis, member) {
...
return await wixData.get("chalets", selChalet)
.then(rowChalet => {
if (rowChalet !== null) {
typeChalet = rowChalet.typeChalet;
return typeChalet;
}
return typeChalet
})
.then(async typeChalet => {
...
while (isPeriodLeft) {
await wixData.query("saison")
.le("fromNum", dateSelFromNum)
.gt("toNum", dateSelFromNum)
.find()
.then(async (saisons) => {
...
if (saisonCount > 0) {
rowsSaison = saisons.items[0];
...
if (dateSelTo <= saisonDateTo) {
...
let nbJours = await numberOfDays(lineDateFrom, lineDateTo);
if (isMember) {
...
let tarif = await wixData.query("tarif")
.eq("saison", saisons.items[0]._id)
.eq("typeChalet", typeChalet)
.eq("famille", selFamMbr)
.eq("group", groupSelected)
// .include("saison", "typeChalet", "group")
.find();
...
if (rowTarifCount > 0) {
subTotal = await calculateSubTotal(rowTarif.prixDay, rowTarif.prixHebdo, rowTarif.prixMensuel, nbJours);
...
periodTarif.push(tbl);
... }
}
...
return periodTarif;
})
.then(period => {
... return period;
})
.catch((err) => {
console.error(err);
const response = {
"isOK": false,
"total": 0,
"period": [{ "?????": "null" }]
};
return period;
})
})
.catch((error) => {
console.error(error);
return response;
});
})
.catch((error) => {
console.error(error);
return response;
});
}
And the front end code:
export async function displayTarif() {
let dollar = new Number();
dollar = 0;
if (selChalet !== null && selFrom !== null && selTo !== null && selFromNum !== null && selToNum !== null && isMember !== null && isInviter !== null && isVisiteur !== null && selFamMbr !== null && selFamInv !== null && selFamVis !== null && member !== null) {
//let response = await
return await calculTarif(selChalet, selFrom, selTo, selFromNum, selToNum, isMember, isInviter, isVisiteur, selFamMbr, selFamInv, selFamVis, member)
// })
.then((period) => {
return period;
})
.then(zzz => {
return getTarifReserv(member)
.then(response => {
...
dollar = cout;
if (dollar === 0 || dollar === undefined || dollar === null) {
$w('#tarif').text = $w('#adminTarif').text = "?";
} else {
let tmp = dollar.toLocaleString();
$w('#adminTarif').text = $w('#tarif').text = tmp;
}
return response;
})
})
.then((period) => {
return SaveRqustTarifItems(selChalet, selFrom, selTo, selFromNum, selToNum, isMember, isInviter, isVisiteur, selFamMbr, selFamInv, selFamVis, member)
.then((period) => {
return period;
})
})
.catch((err) => {
console.error(err);
});
}
if (dollar === 0 || dollar === undefined || dollar === null) {
$w('#tarif').text = $w('#adminTarif').text = "?";
} else {
let tmp = dollar.toLocaleString();
$w('#adminTarif').text = $w('#tarif').text = tmp;
}
return;
}
So there’s a little too much going on here for me to see exactly what is wrong, but to debug where your issue is perhaps start a little more simply.
The first issue is successfully calling your backend function from the FE, so I would pull it out of that big function and isolate it. If the data is returning then the issue is with your FE function.
Something like in your onReady just start with this and see if you can get your data:
import { calcuTarif } from 'backend/myFunctionFile'
$w.onReady(async function () {
const results = await calcuTarif(hardcode, your, parameters,to,test);
console.log(JSON.stringify(results));
});
Start simply with the debugging and it will be easier to pin down the issue.
Instead of returning await calculTarif try setting a variable to the result and then returning that variable at the end. For instance:
let tarif = await caclulTarif(arguments)
… rest of code here
return tarif
big thank you for your advice. All this mess (on my side), was because I forget to initialize some input parameter.