Maps: Use 2 queries - Uncaught (in promise) TypeError: Cannot read property 'length' of undefined

Hey,
I coded the Goole Maps from @yisrael-wix Example: Multiple Markers Google Maps | Corvid by Wix .
But the code only work with one query. When I add a second one, I get an error:

When I click the error code, it marks the " i++ " on my page code.

PAGE CODE
....

function sendLocations() {
    getLocations().then((locations) => {
 let markers = [];
 for (let i = 0; i < locations.length; i++) {
 let loc = locations[i];
            markers.push({ title: loc.title, position: { lat: loc.lat, lng: loc.lng } });
        }
        $w('#htmlMap').postMessage({ markers });
    });
}

Here my locations.jsw from backend:

export function getLocations() {
 return wixData.query("COLLECTION1").and(wixData.query("COLLECTION2"))
        .find()
        .then((results) => {
 return results.items;
        })
        .catch((err) => {
 let errorMsg = err;
        });
}

Does anyone have some ideas ?
Thanks

Okay, the problem is, that “locations” at the page code doesn’t get the value from the backend.

When I change my code to this

    getLocations().then((locations) => {
        locations = [];
 let markers = [];

I don’t get the errorMsg anymore, but I also don’t get any markers.

This statement is invalid:

return wixData.query("COLLECTION1").and(wixData.query("COLLECTION2"))

You can .and two filter conditions, but you can’t .and two queries.

How can I use Google Maps with multiple databases?
I have more than 10 databases.

@nickoertel You can do queries of all of the database collections and then concatenate the results of all of the queries. Something like this:

let allResults = results1.concat(results2, results3, ... resultsX);

However, why do you have so many collections? Why not just use one database collection, and have an additional field which indicates the category or subcategory. It results in a simpler, cleaner design which is easier to use and maintain.

@yisrael-wix Hey, thanks a lot for you help, it works now :grin:
Yeah one collection would be easier to handle, but I’m not the only one working on this project and the others wanted to have it more “clear”.

I wrote 2 different codes to fix my problem. Could you may take a look, if they’re correct and tell me which could is better (if it makes a different).
I would say code1, because it’s much shorter.

CODE 1:

import wixData from 'wix-data';

export async function getLocations() {

 let allResults;

 const results1 = await wixData.query("COLLECTION1")
        .eq("allow", true)
        .find()
        .catch((err) => {
 let errorMsg = err;
        });

 const results2 = await wixData.query("COLLECTION2")
        .eq("allow", true)
        .find()
        .catch((err) => {
 let errorMsg = err;
        });

 return allResults = results1.items.concat(results2.items)
}

CODE 2:

import wixData from 'wix-data';

export async function getLocations() {

 var result1;
 var result2;
 let allResults;

 await wixData.query("COLLECTION1")
        .find()
        .then((results) => {
            result1 = results.items;
 return result1;
        })
        .catch((err) => {
 let errorMsg = err;
        });

 await wixData.query("COLLECTION2")
        .find()
        .then((results) => {
            result2 = results.items;
 return result2;
        })
        .catch((err) => {
 let errorMsg = err;
        });

 return allResults = result1.concat(result2);
}

@nickoertel In CODE2, using await together with .then() is incorrect. I really don’t know what happens there, but I suspect that .then() is ignored (but I’m too lazy to test it out). CODE1 looks good to me. I also like short.

I’m glad you got it working.