I have a 3 collections, and I am trying to run a nested query to obtain results.
The first database is 'projectcontacts', it has 2 fields I am looking at, 'projectId, 'contactId'.
The second database is 'contacts', and I am looking at the '_id', and 'firmId'.
I am simply trying to retrieve 'contacts' database 'firmId'. The 'projectcontacts' database 'contactId' is equal to the 'contacts' database '_id'.
The next step is to pass 'contacts' database 'firmId' to a function to query the third database 'firms' which has various fields but of focus now, the 'firms' database '_id' field is = to 'contacts' database 'firmId'.
I then want to pass some data into a repeater and display only the data from the 'firms' database which has been refined by the previous parameters. Code below:
// Matthew: shows project contacts section
function populateProjectContacts() {
// Matthew: query to build public firms list
let resultFirmsItems = [];
let allFirmsContacts = [];
let pushedFirmsContacts = [];
wixData.query('projectcontacts')
.eq("projectId", selectedProjectId)
.find()
.then((projectContactsForFirmsResults) => {
if (projectContactsForFirmsResults.items.length > 0) {
resultFirmsItems = projectContactsForFirmsResults.items;
for (var i = 0; i < projectContactsForFirmsResults.items.length; i++) {
allFirmsContacts[i] = resultFirmsItems[i].contactId
pushedFirmsContacts.push({ "_id": resultFirmsItems[i]._id, "projectId": resultFirmsItems[i].projectId, "contactId": resultFirmsItems[i].contactId });
let firmItems = [];
let allFirms = [];
let pushedFirms = [];
wixData.query('contacts')
.hasSome("_id", allFirmsContacts)
.find()
.then((contactsForFirms) => {
if (contactsForFirms.items.length > 0) {
firmItems = contactsForFirms.items;
for (var b = 0; b < contactsForFirms.items.length; b++) {
allFirms[b] = firmItems[b]._id;
pushedFirms.push({ "contactId": firmItems[b]._id, "firmId": firmItems[b].contactFirmId });
console.log("firmItems = ")
console.log(firmItems)
console.log("allFirms = ")
console.log(allFirms)
console.log("pushedFirms = ")
console.log(pushedFirms)
queryFirms(pushedFirms, allFirms);
}
} else {
$w('#projectViewErrorContacts').show();
$w('#projectViewErrorContacts').expand();
console.log("0 public firms contacts")
}
});
}
} else {
console.log("NO PROJECT CONTACTS")
}
});
}
This runs into another function:
// Matthew: queries firms associated to project contacts, lists them publicly
function queryFirms(pushedFirms, allFirms) {
let contactFirmId = pushedFirms.firmId;
//pushedFirms.push({ "contactId": firmItems[b]._id, "firmId": firmItems[b].contactFirmId });
console.log("querying firms...")
console.log("with contact firm ID:...")
console.log(contactFirmId)
wixData.query("firms")
.eq("_id", allFirms)
.find()
.then((results) => {
console.log("querying firms complete...")
if (results.items.length > 0) {
console.log("querying firms has results...")
let items = results.items;
$w('#projectViewFirmsRepeater').data = items
showContacts();
} else {
// handle case where no matching items found
}
})
.catch((error) => {
let errorMsg = error.message;
let code = error.code;
});
}
The result at the moment is that all firms are displayed in the repeater, there is no refinement. @Yisrael (Wix) @GOS thank you for helping me before, any idea with this one?