Hi,
I thought I had this running last night, but this morning when I run it I am getting a bizarre error where my .then() and .catch() both run.
I do not understand why the .catch() runs.
I was so tired last night, I am not sure whether I changed the code slightly before logging out, but I can’t see anything wrong today.
Can someone spot it?
The error message is triggering to console, yet the (err) is coming up as undefined.
The .then() is also successfully running as all the data is correctly filled.
const myQuery = wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.opponent)
.eq("opponentID", gameData.player);
await wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.player)
.eq("opponentID", gameData.opponent)
.or(myQuery)
.find()
.then( (queryResult) => {
if (queryResult.items.length <= 0) { //I think we can replace this with query.get instead of .find
errorMessage("Error QP001: There appears to be a problem with the data that was submitted, please contact the system administrator as this error should not happen.");
record.error = true;
} else {
record.dbRecord = queryResult.items[0];
record.submittedRecord = checkExistingScore(gameData,record.dbRecord,status);
if (record.submittedrecord.error) {
errorMessage(record.submittedrecord.error);
record.error = true;
}
// console.log("inside check before return:")
// console.log(record);
}
})
.catch( (err) => {
errorMessage("Critical Error QP002: Pairing data does not exist. Contact the administrator as this should not happen.",err);
record.error = true;
});
return record;
import wixData from 'wix-data';
let gameData = "?????????";
let record = "??????????";
$w.onReady(async function () {
let myQuery = await wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.opponent)
.eq("opponentID", gameData.player);
wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.player)
.eq("opponentID", gameData.opponent)
.or(myQuery)
.find()
.then((res)=> {
if (res.items.length>0) {console.log("RESULTS: ", res);
record.dbRecord = res.items[0];
record.submittedRecord = checkExistingScore(gameData,record.dbRecord,status);
if (record.submittedrecord.error) {
// code...
// code...
// code...
return record;
}
return record;
}
else {console.log("No data found!!!"); return record;}
})
.catch((err)=> {console.log("ERROR: ", err);
record.error = true;
return record;
});
});
How is your errorMessage() function defined?
Thanks for the reponse @russian-dima . I changed the OR part of the query to let and played with swapping where the “await” sits.
I also added the additional returns with no effect.
Particularly, the .catch( (err) ) still triggers despite the (err) being undefined when I log it.
For my own understanding as I am struggling with how to make promise logic work:
-
why would the change to “let” make a difference?
-
why would there be a requirement to change that part of the query to “await”? I see you simply swap where you make it “await”
myQuery is syntax only to make the fully built query more readable unless I am mistaken.
Hi Chris,
It is a placeholder at the moment, but does a log to console every time so that we can see what is happening at critical moments.
function errorMessage(message,error) {
console.log(message);
if (error) {
console.log(error);
}
submitMessage = submitMessage + '<p style="font-size: 16px">' + message + '</p>';
}
For the record, I removed the function call and directly logged to console to test that out, but catch() still triggers with an undefined err value
Oh goodness, I just found it.
After a lot of fiddling, I decided to change from a .catch() to actually processing the rejection as part of the .then().
After which I received a proper error message pointing to result.submittedRecord.
In the quick error check directly after calling checkExistingScore I refer to result.submitted r ecord.error
Small capitalisation error that caused three days of confusion. There are times I understand why TypeScript exists.
This is what I did to have the error come back correctly after which I spotted my foolish typo.
await wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.player)
.eq("opponentID", gameData.opponent)
.or(myQuery)
.find()
.then( (queryResult) => {
if (queryResult.items.length <= 0) { //I think we can replace this with query.get instead of .find > check this later
errorMessage("Error QP001: There appears to be a problem with the data that was submitted, please contact the system administrator as this error should not happen.");
record.error = true;
// return record;
} else {
record.dbRecord = queryResult.items[0];
record.submittedRecord = checkExistingScore(gameData,record.dbRecord,status);
if (record.submittedRecord.error) { //This line is where it all went wrong!
errorMessage(record.submittedrecord.error);
record.error = true;
// return record;
}
// console.log("inside check before return:")
// console.log(record);
}
}, (err) => {
//console.log(err);
errorMessage("Critical Error QP002: Pairing data does not exist. Contact the administrator as this should not happen.",err);
//record.error = true;
//return record;
});
/* })
.catch( (err) => {
console.log(err);
//errorMessage("Critical Error QP002: Pairing data does not exist. Contact the administrator as this should not happen.",err);
record.error = true;
return record;
});
*/
hmm how do i close the question now?
Good catch! (pun intended).
For the record, if you’re using async/await in your code, you don’t need to use .then() and .catch(), as it can be written a little cleaner, in my opinion, using await without the then callback. For example, the above code block could look like:
try{
const queryResult = await wixData.query("Pairings")
.eq("pod", gameData.pod)
.eq("playerID", gameData.player)
.eq("opponentID", gameData.opponent)
.or(myQuery)
.find();
if (queryResult.items.length <= 0) { //I think we can replace this with query.get instead of .find > check this later
errorMessage("Error QP001: There appears to be a problem with the data that was submitted, please contact the system administrator as this error should not happen.");
record.error = true;
} else {
record.dbRecord = queryResult.items[0];
record.submittedRecord = checkExistingScore(gameData,record.dbRecord,status);
if (record.submittedRecord.error) {
errorMessage(record.submittedrecord.error);
record.error = true;
}
}
catch(err){
errorMessage("Critical Error QP002: Pairing data does not exist. Contact the administrator as this should not happen.",err);
}
@charl-holtzhausen
Sorry was not here for a while, but it seems that problem already resolved.
@chris-derrell Thanks Chris. I have been trying to get the async functionality in my head. It feels like I read the documentation on promises twice a week and the more I use it, the more what they say makes sense.
This can certainly be closed now for the purpose of the original question, but if you have the time I have one more question:
Let’s say I take the above code and want everything that was supposed to execute in the .then() into a function to make it read easier. How do you pass the result values across to the function for both a completed promise and a rejected promise?
query.find()
query.then( successFunction, rejectFunction)
Documentation essentially gives me this, but what would it look like to be able to reference the values?