Can i query and insert into 2 databases at the same time?

Ive tried the following code to query and insert into 2 db’s, however it only inserts into 1?

.then((user) => {
                userId = user.id;
 return user.getEmail();
            })
            .then((email) => {
 // check if there is an item for the user in the collection
                userEmail = email;
 return wixData.query("ApproveApplication") &&("MemberProfile")
                    .eq("_id", userId)
                    .find();
            })
            .then((results) => {
 // if an item for the user is not found
 if (results.items.length === 0) {
 // create an item
 const toInsert = {
 "_id": userId,
 "email": userEmail
                    };
Promise.all([
wixData.get("ApproveApplication", userId),
wixData.get("MemberProfile", userId)
])
.then(r => {
if(!r[0] && !r[1]){
//in case it doesn't appear in both collection, write here what to do
}
})

[EDITED]

hi buddy i tried that code snippet above within my site code and it didn’t insert into any collections! this is what i put

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
// import { displayNotifications } from 'public/SK-Notes';

/*
const hasNotifications = 'wix:vector://v1/848f2af4c8f14a6aac59be8255ce9b83.svg/848f2af4c8f14a6aac59be8255ce9b83.svg',
    hasNone = 'wix:vector://v1/848f2af4c8f14a6aac59be8255ce9b83.svg/848f2af4c8f14a6aac59be8255ce9b83.svg',
    slideOptions = {
        "duration": 1200,
        "direction": "top"
    };
*/
let usrID;

$w.onReady(() => {
 // displayNotifications('notDropdown', {
 //  trigger: 'icon',
 //  display: 'counts',
 //  has: hasNotifications,
 //  notHas: hasNone,
 //  animation: slideOptions,
 // });
 if (wixUsers.currentUser.loggedIn) {
        $w("#button4").label = "Logout";

        $w("#button5").show();
        usrID = wixUsers.currentUser.id;
    } else {
        $w("#button4").label = "Trade's Login";

        $w("#button5").hide();
    }
});

export function button4_click(event) {
 // user is logged in
 if (wixUsers.currentUser.loggedIn) {
 // log the user out
        wixUsers.logout()
            .then(() => {
 // update buttons accordingly
                $w("#button4").label = "Trade's Login";
                $w("#button5").hide();
            });
    }
 // user is logged out
 else {
 let userId;
 let userEmail;

 // prompt the user to log in 
        wixUsers.promptLogin({ "mode": "login" })
            .then((user) => {
                userId = user.id;
 return user.getEmail();
            })
            .then((email) => {
 // check if there is an item for the user in the collection
                userEmail = email;
                Promise.all([
                        wixData.get("ApproveApplication", userId),
                        wixData.get("MemberProfile", userId)
                    ])
                    .then(r => {
 if (!r[0] && !r[1]) {

 const toInsert = {
 "_id": userId,
 "email": userEmail
 //in case it doesn't appear in both collection, write here what to do
                                }
 
 

 // add the item to the collection
                        wixData.insert("", toInsert)
                        .catch((err) => {
                            console.log(err);
                        });
                    }
 // update buttons accordingly
                $w("#button4").label = "Logout";
                $w("#button5").show();
            })
            .catch((err) => {
                console.log(err);
            });
    }

            )}}

export function button5_click() {
    wixLocation.to(`/account/my-account`);
}

Add return before the Promise.all. Also put the collection name in the .insert()

hi thanks buddy that code works but sometimes doesn’t, have you any ideas?

You’ll need to try location the problem.
add .console.log() with the relevant data to every step + add return before the wixData.insert + add .then(r => console.log(r)) after the insert(), and try to figure it out.

Hi JD still no luck buddy, i’m actually unable to preview the login and get an error code because i use the standard wix login feature and not a custom page, i tried the above but still didnt work

Hi Jd ive managed to get it working to a degree off a register success lightbox and not the site code above, it inserts into 2 databases the same id’s and owner id’s however it seems when first registering it also adds an Owner user Id automatically to the backend, and Members/PrivateMemberData, so because im using two different datasets linked to 2 buttons within the members area that are both filtered to owner is logged in, whats happening is the 1 dataset linked to the first db is pulling the original signup Id and not the one being inserted

register success lightbox code

import wixSite from 'wix-site';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import wixUsers from 'wix-users';


let usrID;

$w.onReady(function () {
 let registerSuccessBox = wixSite.currentPage;

});

export function lightbox1_viewportEnter(event) {
 
 let user = wixUsers.currentUser;

 let userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
 let isLoggedIn = user.loggedIn; // true

        user.getEmail()
            .then((email) => {
 let userEmail = email; // "user@something.com"
 
 
 

 // create an item
 const toInsert = {
 "_id": userId,
 "email": userEmail

    };
 //in case it doesn't appear in both collection, write here what to do

    wixData.insert("ApproveApplications", toInsert)
        .then((results) => {
 let item = results;
        })
        .catch((err) => {
 let errorMsg = err;
        });

        wixData.insert("MemberProfile", toInsert)
        .then((results) => {
 let item = results;
        })
        .catch((err) => {
 let errorMsg = err;
        });

            })}

export function proceedButton_click(event) {
    wixLocation.to(`/account/my-account`);

}

Here you can you see the id at the bottom of page when hovering over My Profile button


Is different to the id when hovering over the Create Profile Button,

well I need both the Id’s to match?

It looks like i have to fetch the original signup id within the PrivateMembersData and insert it into the second Db but i don’t know how!