Hi,
Hope someone could shed some light on my problem/confusion/frustration.
I have a backend function (below) to insert a reference into a table with a multi reference field, the function itself works fine.
export function RegisterPromotion(CustomerID, PromotionID){
return wixData.query("PromotionsCustomer")
.eq("_id", PromotionID)
.find()
.then((results) => {
const CodeValidity = 20
const today = new Date();
let PromotionName = results.items[0].title;
let CodePrefix = results.items[0].templateCode
let RegisteredDate = FormatDate(today);
var ExpiryDate = new Date(today);
ExpiryDate.setDate(today.getDate() + CodeValidity);
CodePrefix = CodePrefix + GenerateRandomNumber(5)
//
// other code removed to simplify explanation
//
wixData.insertReference("Customer", "registeredPromotions", CustomerID,PromotionID)
.then(() => {
return true // how to return a result ??
})
.catch((err) => {
let errorMsg = err;
});
});
}
An example of a call to the function, I have only shown the code to call the function and the declaration
import {RegisterPromotion} from 'backend/support';
// ….
RegisterPromotion(CustomerID, PromotionID).then(function (result) {
console.log(result)
});
I have tried various ideas, but I can only get a return value of undefined???
I simply want to be able to return the result of the insertReference, or another value I define, such as true? To avoid any misunderstanding, calling the function, and the function itself works as expected.
I understood there are various nested promises (the .query then the .insertReference) inside the RegisterPromotion function, I thought I needed to have a return at the start of the chain of promises, but that did not work. I also understand calling the backend function itself also involves a promoise. Finally, I tried returning true on the success result of the .insertReference promise, again with no success.
Now I am lost, excuse my ignorance with the use of promises.
Any help appreciated!