Hi everyone,
I’m having some difficulty getting the wixData.query() function to work as intended.
In summary what I’m looking for my code to do is as follows…
When continueButton is clicked, contentCollection should be queried to find the item where the _owner field matches the current user ID (there is only one such item in contentCollection for each user). Then, if such an item exists, the item field updateField should be updated with the value of an element on the page. Otherwise, if such an item doesn’t exist, a warning message should be returned.
Currently all I’m seeing in the log is the message “Returned nothing”, so from what I can tell, nothing contained in the then() promise is being executed and so the query isn’t working.
I’ve reviewed the Velo documentation here and followed the fourth code example as it appears to do exactly what I’m after, but had no luck so far.
Any thoughts on where I’m going wrong please?
Thanks in advance!
export function continueButton_click(event) {
console.log("continueButton clicked")
wixData.query('contentCollection')
.eq('_owner', wixUsers.currentUser.id)
.find()
.then( (results) => {
console.log("Queried collections")
if (results.items.length > 0) {
let item = results.items[0];
item.updateField = $w('#element').value;
wixData.update('contentCollection', item);
console.log('User's item has been updated');
} else {
$w('#warningMessage').show();
}
})
.catch( (err) => {
let errorMsg = err;
console.log("Returned nothing")
});
wixLocation.to('/next-page');
}
@jake5101 In the .catch section, put a console.log that will inform you of the error. The message “Returned nothing” would be appropriate in the else part of the if statement above where the query ran without errors but returned no records.
console.log("Error Message:",errorMsg);
Hi @anthonyb, thanks for your response.
I’ve changed the console.log as you suggested, but when I view the console I’m not seeing any error message returned (screenshot of what I see below). Any ideas why that might be please?
@jake5101 I’ve copied and pasted the whole catch section, so there is no doubt where it needs to go.
.catch((err)=>{
let errorMsg = err;
console.log("Error Message:",errorMsg);
});
Also, right after .then, put in another console.log to capture the array results, so you can see what the query is returning when it runs without errors.
.then((results)=>{
console.log("Queried collections");
console.log(results);
if ...
@anthonyb thanks for your help.
I realised the issue I was facing in the end wasn’t actually to do with the code, and was in fact due to the permissions I’d set on my collection. A stupid mistake maybe haha, but one I wouldn’t have realised without your help, so thanks!
The code that worked is as you suggested and shown below if anyone else faces a similar issue.
export function continueButton_click(event) {
console.log("continueButton clicked")
wixData.query('contentCollection')
.eq('_owner', wixUsers.currentUser.id)
.find()
.then((results) => {
console.log("Queried collection:", results)
if (results.items.length > 0) {
let item = results.items[0];
item.updateField = $w('#element').value;
wixData.update('contentCollection', item)
console.log("User's item has been updated");
} else {
$w('#warningMessage3').show();
console.log("Returned nothing");
}
})
.catch((err) => {
let errorMsg = err;
console.log("Error message:", errorMsg);
});
wixLocation.to('/next-page');
}