Question:
When I try to reuse the query from a result by adding a .eq
condition it fails with TypeError: query.eq is not a function
Velo, Wix Editor
This is the code:
$w('#topicDropdown').onChange((event) => {
const resources = getRouterData();
const resourceTopicId = $w('#topicDropdown').value;
// BEGIN FAILING_CODE
let query = resources.query;
console.log("query", query);
try {
query.eq("resourceTopic", resourceTopicId)
.find()
.then(results => {
$w("#resources").data = results.items;
})
.catch((error) => {
console.log("error setting resource data to topic", error);
});
} catch (error) {
console.log("error reusing query", error); // TypeError: query.eq is not a function
}
// END FAILING_CODE
// Copying the query code from the router works fine
wixData
.query("ResourceLink")
.eq("resourceTopic", resourceTopicId) // added to code of original query from the router in routers.js
.include("resourceSubject")
.include("resourceTopic")
.include("resourceSubtopic")
.include("resourceCategory")
.ascending("sortOrder")
.limit(200)
.find()
.then(results => {
$w("#resources").data = results.items;
})
.catch((error) => {
console.log(error);
});
})
and this is the json of what is logged for query
{
"orderBy": [
{
"sortOrder": "asc"
}
],
"invalidArguments": [],
"filterTree": {},
"encoder": {},
"validateCollectionName": true,
"provider": {
"cloudDataUrl": "",
"gridAppId": "e7c12230-e6b3-4623-9fd1-443c87d95cba",
"environment": "SANDBOX_PREFERRED"
},
"collectionName": "ResourceLink",
"limitNumber": 200,
"skipNumber": 0,
"included": [
"resourceSubject",
"resourceTopic",
"resourceSubtopic",
"resourceCategory"
],
"projectedFields": []
}
You shouldn’t expect the .catch()
method to be reached if the line before it wasn’t
Try surrounding the failing code with a try/catch
statment
try {
// Failing code
} catch (error) { console.error(error) }
I know, I was just including all my code. In any case, I did try wrapping the whole failing block in a try catch, and the result is query.eq
is not a function, which is odd because Velo thinks it is (i.e. it autocompletes it.
I’ll update my question with that information…
EDIT: it’s just the generic autocomplete. In any case, the documentation indicates the query from a WixDataQueryResult should be a usable query object: Query | Velo
I’ve reworked the question, thanks
Try removing the semicolons after every query. That is most likely the reason why your queries are not getting chained as they should, and instead being recognised individual functions, which in turn yields this result:
I don’t see how that is relevant(?)
Well, I removed the chaining so that I could log the query object. There’s no difference between:
const query = resources.query;
...
query.find()
and
resources.query.find()
I’m sorry, I can’t tell from the threading which thing you are saying is irrelevant.
Pratham’s message, the ;
has no relevance, without it the compiler simply puts one itself
Dean is right, removing that isn’t yielding anything useful either.
I tried recreating the functionality on my end, and I am facing the same roadblock despite the way it is shown in the documentation.
So I would suggest reconsidering your logic flow to something that maybe does not require rolling over queries to another page, since the only solution I see for that now is for you to have some kind of an array map which co-relates to each of your queries that can be transferred to the next page. The code on the next page will have to loop through each item and chain it to the query based on whether it’s .eq or .include… etc., somewhat like the filterTree object that is returned results. Not the best and most straightforward way, I know. But just a possible workaround.