Hello everyone, I came across this problem: I have to take out of a query that I often use values so I do not have to always call them back.
This is my code:
let index = null;
let idRows = null;
let AttivoRows = null;
let NameRows = null;
let DataRows = null;
let TipoRows = null;
let CodeRows = null;
let UtenteRows = null;
let RedirectRows = null;
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
wixData.query("RedirectCerimonie", )
.eq('_id', idRows)
.find()
.then( (results) => {
let items = results.items;
let item = items[0];
DataRows = item.dataEvento;
TipoRows = item.tipoEvento;
NameRows = item.nomeEventoOErimonia;
CodeRows = item.code;
UtenteRows = item.utenteRedirect;
RedirectRows = item.redirect;
AttivoRows = item.attivo;
console.log('--------Log 01--------------');
console.log('DataRows- ' + DataRows);
console.log('TipoRows- ' + TipoRows);
console.log('NameRows- ' + NameRows);
console.log('CodeRows- ' + CodeRows);
console.log('UtenteRows- ' + UtenteRows);
console.log('RedirectRows- ' + RedirectRows);
console.log('AttivoRows- ' + AttivoRows);
console.log('-------------------------------------------');
})
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
});
buttomActivated();
console.log('---------------Log 02----------------------');
console.log('DataRows- ' + DataRows);
console.log('TipoRows- ' + TipoRows);
console.log('NameRows- ' + NameRows);
console.log('CodeRows- ' + CodeRows);
console.log('UtenteRows- ' + UtenteRows);
console.log('RedirectRows- ' + RedirectRows);
console.log('AttivoRows- ' + AttivoRows);
console.log('---------------------------------------');
}
By logging in 01 I see that my query works perfectly, giving me the search results. The log 02 just outside the query does not work, I get back underfined.
How can I solve the problem?
Thank you
Hello,
sorry I'll answer you only now but trying to understand reading between forums and more how it works
I have not yet come to understand the process well, but I try to find a solution with more skills
Thank you
In the above code snippet, when are the variables (DataRows, TypeRows, NameRows, etc) set? The only values that are available in those variables would be from the last query.
Sorry, but I really don't understand your logic and what you are trying to do.
The main thing to understand in all of this is that until the Promise is fulfilled (.then), you don't have any results. The results are only returned in the .then.
So these variables when I'm sure they become available?
for example if under one of the two scripts seen before I put this
export function serviceUpdate_click () {
if ((index! = undefined) || (index! == null)) {
$ w ("# ImptData"). value = DataRows;
$ w ("# ImptEvent"). value = TypeRows;
$ w ("# ImputName"). value = NameRows;
$ w ("# ImptCode"). value = CodeRows;
$ w ("# ImpuUser"). value = UserRows;
$ w ("# ImputUrl"). value = RedirectRows;
$ w ("# inputActivate"). value = AttivoRows;
}
}
I'm sure that even if I have a collection with lots of lines, the variables will always be filled with the query made? I hope I explained myself
If they both have the same results, then I guess so.
Without testing them they seem the same, I tested them and I did not see any differences. It's correct?
Without testing them, they seem to be the same.
Did you try them both?
Thanks Yisrael, sorry
so these two codes are the same or different? just to understand
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
wixData.query("RedirectCerimonie", )
.eq('_id', idRows)
.find()
.then( (results) => {
let items = results.items;
let item = items[0];
DataRows = item.dataEvento;
TipoRows = item.tipoEvento;
NameRows = item.nomeEventoOErimonia;
CodeRows = item.code;
UtenteRows = item.utenteRedirect;
RedirectRows = item.redirect;
AttivoRows = item.attivo;
})
function QueryData (idRows) {
return wixData.query("RedirectCerimonie")
.eq('_id', idRows)
.find()
.then( (results) => {
return results.items;
});
}
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
QueryData(idRows)
.then ((items) => {
let item = items[0];
DataRows = item.dataEvento;
TipoRows = item.tipoEvento;
NameRows = item.nomeEventoOErimonia;
CodeRows = item.code;
UtenteRows = item.utenteRedirect;
RedirectRows = item.redirect;
AttivoRows = item.attivo;
});
}
Thank You
In QueryData(), you need to return a promise and not the results. Just add a return:
function QueryData (idRows) { return wixData.query("RedirectCerimonie") // return needed here .eq('_id', idRows) .find() .then( (results) => { return results.items; }); }
Try this and see if it helps.
Yisrael
Hi Yisrael,
thanks for your answer. This thing is difficult for me to understand. However, I rewrote the code following your instructions and the guide I had already seen, but continues to give me error:
function QueryData (idRows) {
wixData.query("RedirectCerimonie")
.eq('_id', idRows)
.find()
.then( (results) => {
return results.items;
});
}
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
QueryData(idRows)
.then ((items) => {
let item = items[0];
console.log(item);
});
}
This is error that I get
TypeError: QueryData(...) is undefined
Thank tou
Hey Daniel,
Let's look at TEST 03...
You call QueryData() expecting immediate results:
let results = QueryData(idRows);
However, the results in QueryData() are returned in the .then() of a Promise:
.then( (results) => { return results.items; // doesn't get to here first } ); // gets here first, and this is where QueryData() REALLY returns
The QueryData() routine actually returns before the .then() is executed.
So, when you call QueryData() from TabEventi_rowSelect(), you should expect it to return a Promise, but not results:
QueryData(idRows) .then( (items) => { let item = items[0]; console.log(item); // item is displayed in the console // you can now get the fields of the item (row) DataRows = item.dataEvento; TipoRows = item.tipoEvento; ... });
A full discussion of this is contained in the section Returning in a .then( ) of the article Working with Promises in Wix Code.
Good luck,
Yisrael
Thanks for the reply. I've read both posts, but I could not get to the end:
I report the different tests done with async / wait. On the comments I wrote, as I understand it, it should work
let index = null; let idRows = null; let AttivoRows = null; let NameRows = null; let DataRows = null; let TipoRows = null; let CodeRows = null; let UtenteRows = null; let RedirectRows = null;
async function QueryData (idRows) {
const results = await wixData.query("RedirectCerimonie", ) .eq('_id', idRows) .find();
// WAIT THE RESULT OF QUERY AND AFTER DO THIS let items = results.items; let item = items[0];
// THESE SHOULD BE BEING GLOBAL VARIABLES DataRows = item.dataEvento; TipoRows = item.tipoEvento; NameRows = item.nomeEventoOErimonia; CodeRows = item.code; UtenteRows = item.utenteRedirect; RedirectRows = item.redirect; AttivoRows = item.attivo;
// THIS CONSOLE.LOG WORK console.log('-----------TabEventi_rowSelect inside---------------'); console.log('DataRows- ' + DataRows); console.log('TipoRows- ' + TipoRows); console.log('NameRows- ' + NameRows); console.log('CodeRows- ' + CodeRows); console.log('UtenteRows- ' + UtenteRows); console.log('RedirectRows- ' + RedirectRows); console.log('AttivoRows- ' + AttivoRows); console.log('----------------------------------------------------'); }
export function TabEventi_rowSelect(event) { index = event.rowIndex; idRows = event.target.rows[index]._id; QueryData (idRows);
// THIS CONSOLE.LOG NOT WORK. Global variables should have been set because with async / await I waited for the query result
buttomActivated(); console.log('---------------TabEventi_rowSelect out----------------------'); console.log('DataRows- ' + DataRows); console.log('TipoRows- ' + TipoRows); console.log('NameRows- ' + NameRows); console.log('CodeRows- ' + CodeRows); console.log('UtenteRows- ' + UtenteRows); console.log('RedirectRows- ' + RedirectRows); console.log('AttivoRows- ' + AttivoRows); console.log('-------------------------------------'); }
let index = null;
let idRows = null;
let AttivoRows = null;
let NameRows = null;
let DataRows = null;
let TipoRows = null;
let CodeRows = null;
let UtenteRows = null;
let RedirectRows = null;
async function QueryData (idRows) {
const results = await wixData.query("RedirectCerimonie", )
.eq('_id', idRows)
.find();
return results.items;
}
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
// RECEIVE THE RESULT OF THE QUERY WAITING THAT THIS HAS BEEN PERFORMED
let results = QueryData (idRows);
//CHECK FOR THE VARIABLES THE VALUES I NEED
let items = results.items;
let item = items[0];
DataRows = item.dataEvento;
TipoRows = item.tipoEvento;
NameRows = item.nomeEventoOErimonia;
CodeRows = item.code;
UtenteRows = item.utenteRedirect;
RedirectRows = item.redirect;
AttivoRows = item.attivo;
// THIS CONSOLE.LOG NOT WORK. Error in console.log when run: TypeError: items is undefined
buttomActivated();
console.log('---------------TabEventi_rowSelect out----------------------');
console.log('DataRows- ' + DataRows);
console.log('TipoRows- ' + TipoRows);
console.log('NameRows- ' + NameRows);
console.log('CodeRows- ' + CodeRows);
console.log('UtenteRows- ' + UtenteRows);
console.log('RedirectRows- ' + RedirectRows);
console.log('AttivoRows- ' + AttivoRows);
console.log('-------------------------------------');
}
This is the test instead I did with then, but even this does not work:
let index = null;
let idRows = null;
let AttivoRows = null;
let NameRows = null;
let DataRows = null;
let TipoRows = null;
let CodeRows = null;
let UtenteRows = null;
let RedirectRows = null;
function QueryData (idRows) {
wixData.query("RedirectCerimonie", )
.eq('_id', idRows)
.find()
.then( (results) => {
return results.items;
} );
}
export function TabEventi_rowSelect(event) {
index = event.rowIndex;
idRows = event.target.rows[index]._id;
let results = QueryData(idRows);
let items = results.items;
let item = items[0];
DataRows = item.dataEvento;
TipoRows = item.tipoEvento;
NameRows = item.nomeEventoOErimonia;
CodeRows = item.code;
UtenteRows = item.utenteRedirect;
RedirectRows = item.redirect;
AttivoRows = item.attivo;
// THIS CONSOLE.LOG NOT WORK. Error in console.log when run: TypeError: results is undefined
console.log('---------------TabEventi_rowSelect out----------------------');
console.log('DataRows- ' + DataRows);
console.log('TipoRows- ' + TipoRows);
console.log('NameRows- ' + NameRows);
console.log('CodeRows- ' + CodeRows);
console.log('UtenteRows- ' + UtenteRows);
console.log('RedirectRows- ' + RedirectRows);
console.log('AttivoRows- ' + AttivoRows);
console.log('-------------------------------------');
}
I think the error is the same on both tests, but I do not know what it is
Where am I wrong?
ThankYou
Hi Daniel,
The problem with the log 02 is that it doesn't run after the query is finished - it actually executes before the query returns results.
Take at look the Promises, Promises forum post for a simple explanation of what happens with Promises.
I hope this helps,
Yisrael
Consider using async/await as described here: https://support.wix.com/en/article/working-with-promises-in-wix-code