Hi everyone !!!
I am having a query which I thought and thought, coded and coded, but didn’t get the solution
Here it is ~>
- Query one database.
- Get the results.
- Query the results to another database.
- Assign it to repeater’s data.
For example ~>
After the first query of the database “numbers”, the result got is - > ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘6’
Then, I want to query the result (‘1’ ‘2’ ‘3’ ‘4’ ‘5’ ‘6’) to other database called ~> “digits”.
Then, get the results.
Then, assign it to the repeater’s data.
Thanks!
Ajith
You can use the result from the query of the first collection in a .hasSome() or .hasAll() filter. Not sure if that’s what you want, but that’s one example of what you could do.
Thanks for your answer Yisrael!!
What I am trying do, in simple words, is - >
Pass results of one query to another query.
@ajithkrr OK - and in simple words… my previous comment was one example of how you could do that. Get the results of the first query, create an array of the values from the result, and then do a filter on the query of the second collection using the array of values from the first query.
@yisrael-wix Here’s my code, it is not working properly →
$w.onReady( function () {
let user = wixUsers.currentUser;
let userId1 = user.id;
let allitems = [];
let ucitems = []
let allMessages = [];
let myNewData = [];
//console.log(userId1);
wixData.query("UserContacts")
.eq("userId", userId1)
.find()
.then((results) => {
ucitems = results.items;
for (var i = 0; i < results.items.length; i++) {
allMessages[i] = [ucitems[i].userUserid]
myNewData.push({ "_id": ucitems[i]._id, "userUserid": ucitems[i].userUserid});
}
wixData.query("allUsers")
.hasAll( [allMessages])
.find()
.then((resultsi) => {
let items2 = resultsi.items;
console.log("items2 " + JSON.stringify(items2));
$w('#repeater1').data = items2;
});
});
});
Thanks!
@ajithkrr Believe me, tagging me three times is not going to help…
You need to include your second query inside the .then() of your first query. Or, call your queries with await . If you need help understanding asynchronous code execution, read the following articles:
Your .hasAll() filter is incorrect, It should be without the square bracket. Something like this:
.hasAll(allMessages)
@yisrael-wix Believe me , tagging you 3 times was accidental ,
Any way, here is my(your) code.
As you said I included the second query inside my first query.
I also removed the bracket, yet it is not working
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId1 = user.id;
let allitems = [];
let ucitems = []
let allMessages = [];
let myNewData = [];
wixData.query("UserContacts")
.eq("userId", userId1)
.find()
.then((results) => {
ucitems = results.items;
for (var i = 0; i < results.items.length; i++) {
allMessages[i] = [ucitems[i].userUserid]
myNewData.push({ "_id": ucitems[i]._id, "userUserid": ucitems[i].userUserid });
}
wixData.query("allUsers")
.hasAll(allMessages)
.find()
.then((resultsi) => {
let items2 = resultsi.items;
console.log("items2 " + JSON.stringify(items2));
$w('#repeater1').data = items2;
});
});
});
The console is empty
The console is probably empty since your code is incorrect. You have a number of errors in your Javascript. As an example:
allMessages[i] = [ucitems[i].userUserid]
The errors are probably causing your code to terminate. In addition, you also need an onItemReady() function for your Repeater.
You will need to debug your code to determine where to find why it isn’t running. See the article Corvid: Testing and Debugging Your Code. to learn the techniques of debugging Javascript and Corvid.
We are unable to provide full code solutions. Wix provides many resources to help users learn how to get the most out of Corvid. First, you will need to learn the basics of Javascript. I would suggest visiting the following sites for information on programming in Javascript:
To learn about programming with Corvid, read the following articles that will help you start working with Corvid:
-
About Corvid by Wix - what Corvid is and what features it has.
-
Getting Started with Corvid by Wix - step-by-step tutorial on how to start using Corvid.
-
Onboarding to Corvid by Wix - introduction to Corvid with short tutorials.
If you find that you are having difficulty with code and need assistance, you may want to check out the Wix Marketplace - it’s a place where you can look for Corvid experts for hire.
Hi Yisrael,
I am getting the result , exactly as I want, here->
for (var i = 0; i < results.items.length; i++) {
allMessages[i] = [ucitems[i].userUserid]
myNewData.push({ "_id": ucitems[i]._id, "userUserid": ucitems[i].userUserid });
}
console.log(allMessages);
Console →
But what goes wrong is in the second query…
@ajithkrr
This is incorrect (square brackets are used for an index to an array):
allMessages [i] = [ ucitems [i]. userUserid ]
you want:
allMessages[i] = ucitems[i].userUserid
Your .hasAll() filter is incorrect, you need to specify a field for the filter. See the .hasAll() API.
If you want to retrieve all records that match the userIds in the allMessages array, then you probably want to use .hasSome() instead. You need to specify a field in the collection.
Something like this:
. hasAll ( field, allMessages )
@yisrael-wix You are the best !!!
Here is the final code ~>
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId1 = user.id;
let ucitems = []
let allMessages = [];
let myNewData = [];
wixData.query("UserContacts")
.eq("userId", userId1)
.find()
.then((results) => {
ucitems = results.items;
for (var i = 0; i < results.items.length; i++) {
allMessages[i] = ucitems[i].userUserid
myNewData.push({ "_id": ucitems[i]._id, "userUserid": ucitems[i].userUserid});
}
console.log(allMessages);
console.log(myNewData);
wixData.query("allUsers")
.hasSome("userId", allMessages)
.find()
.then((resultsi) => {
let items2 = resultsi.items;
console.log("items2 " + JSON.stringify(items2));
$w('#repeater1').data = items2;
});
});
});
Thanks Yisrael!!
Here is the final code ~>
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId1 = user.id;
let ucitems = []
let allMessages = [];
let myNewData = [];
wixData.query("UserContacts")
.eq("userId", userId1)
.find()
.then((results) => {
ucitems = results.items;
for (var i = 0; i < results.items.length; i++) {
allMessages[i] = ucitems[i].userUserid
myNewData.push({ "_id": ucitems[i]._id, "userUserid": ucitems[i].userUserid});
}
console.log(allMessages);
console.log(myNewData);
wixData.query("allUsers")
.hasSome("userId", allMessages)
.find()
.then((resultsi) => {
let items2 = resultsi.items;
console.log("items2 " + JSON.stringify(items2));
$w('#repeater1').data = items2;
});
});
});