Something is wrong in your logic or in mine xD
Ok, look at this simple example!
You start like always with this one…–> you put the rest of your code inside this one…
$w.onReady(function() { ....... });
For example your code here →
const data = wixData.get("Collection", itemid)
.then( (results) => {
let output = results;
return output;
});
—> turning into a function…
function yourFunction() {
const data = wixData.get("Collection", itemid)
.then( (results) => {
let output = results;
return output;
});
}
—>after doing little corrections…
function yourFunction() {
const data = wixData.get("Collection", itemid)
data.find()
.then((results)=>{return results;})
.catcht((error)=>{return(error)});
}
First part-result would be something like this…
Till here everything clear? What do you think, would this already work?
import wixData from 'wix-data';
$w.onReady(function(){
let DATABASE = "hereYourCollectionID" //example-collection-ID
let itemID = "123213213213213213213" //example-itemID
yourFunction(DATABASE, itemID); // <----starting your function here
// or doing the same with direct-value-injection....
yourFunction("hereYourCollectionID", "123213213213213213213");
});
function yourFunction(DATABASE, itemid) {
wixData.get(DATABASE, itemid)
.then((results)=>{return results;}) //<<--- this here is the important part!
.catch((error)=>{return(error)});
}
Doing it like shown above, you will surely get one more time → an unresolved PROMISE!
So do we still missing? We still have to wait for our RESULTS from the function!
Doing the following steps and upgrading our code, either by an usage of :
- ASYNC-AWAIT
…or… - THEN-METHOD
Let’s do it with async-await → my favourite one…
import wixData from 'wix-data';
$w.onReady(function(){
let DATABASE = "hereYourCollectionID" //example-collection-ID
let itemID = "123213213213213213213" //example-itemID
let myRESULTS = awaityourFunction(DATABASE, itemID);// <--starting your function here
//that means --> only when --> myRESULTS <-- are ready --> you will get the OUTPUT
//OUTPUT
console.log(myRESULTS)
});
function yourFunction(DATABASE, itemid) {
wixData.get(DATABASE, itemid)
.then((results)=>{return results;}) //<<--- this here is the important part!
.catch((error)=>{return(error)});
}
And the last additional step would be to add → “async” to the right place of our generated code…
$w.onReady(async function(){..........
Our END-RESULT-FUNCTION…
import wixData from 'wix-data';
$w.onReady(async function(){
let DATABASE = "CollectionID"
let itemID = "itemID"
let myRESULTS = await yourFunction(DATABASE, itemID); console.log(myRESULTS)
});
function yourFunction(DATABASE, itemid) {
wixData.get(DATABASE, itemid)
.then((results)=>{return results;}) //<<--- this here is the important part!
.catch((error)=>{return(error)});
}
The only thing where i also have to test it, or where i am also not always 100% sure, is this one…
without an extra RETURN…
wixData.get(DATABASE, itemid)
…or with an extra return…
return wixData.get(DATABASE, itemid)
Thats it!