Backend Members/PrivateMembersData Query from separate database to emailUser

I searched, don’t think this has been asked before.

I have a db with user generated content. Due to my error… some of the content wasn’t saved, but fields were created with some content.

I’d like to query the content db for the blank cells to find the owner ID (working), then run a backend query of the Members/PrivateMembersData db to get each creators email/user ID and send a triggered email (not working).

My code is as follows:

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import { userQuery } from 'backend/applicationCalc';
import { emailUser } from 'backend/applicationCalc';

$w.onReady(function () {

    console.log("running Online Registration query")

    wixData.query("OnlineCPDAttendees")
        .eq("title", null)
        .limit(200)
        .find()
        .then((results) => {

 let totalCount = results.totalCount;
            console.log("Query has been run, total count = " + totalCount)

 if (results.items.length > 0) {

                console.log("as query is greater than zero, running backend user query")

 let owner = results._owner;
 let date = results._createdDate
 let items = results.items;

                userQuery(owner);

            } else {
                console.log("ZERO RESULTS IN QUERY")
            }

        })

        .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;

            console.log("ERROR IN QUERY = " + errorMsg + "ERROR CODE = " + code)
            console.log(code)
            console.log(errorMsg)
        });

});

The backend code is as follows:

import wixUsers from 'wix-users';
import wixData from 'wix-data';

export function userQuery(owner) {

 return wixData.query("Members/PrivateMembersData")
        .contains("_id", owner)
        .find()
        .then((userResults) => {

 if (userResults.items.length > 0) {

 let firstName = userResults.firstName;
 let email = userResults.loginEmail;
 let userId = userResults._id;

 return console.log("query run with the following results: " + firstName, email, userId);

            } else {

 return console.log("ZERO RESULTS IN USER DB QUERY")
            }

        })
        .catch((error) => {

 let errorMsg = error.message;
 let code = error.code;

 return console.log("ERROR IN USER QUERY = " + errorMsg + "USER ERROR CODE = " + code)

        })

}

export function emailUser(userId, firstName) {

    wixUsers.emailUser('SO7WKf0', userId, {

            variables: {
                name: firstName
            }

        })

        .then(() => {
 return console.log("emails sent");

        })

        .catch((err) => {
 return console.log("EMAIL ERROR - NOT SENT, error = " + err);

        });

}

It’s failing with the Members/PrivateMembersData query, and I’m not quite sure if I’ve reached the limits of my abilities. @Yisrael (Wix) , any idea?

Hi Spamushere,

First things first :

wixData.query("OnlineCPDAttendees")
        .eq("title", null)
        .limit(200)
        .find()
        .then((results) => {

 let totalCount = results.totalCount;
            console.log("Query has been run, total count = " + totalCount)

 if (results.items.length > 0) {

                console.log("as query is greater than zero, running backend user query")

 let owner = results._owner;
 let date = results._createdDate
 let items = results.items;

                userQuery(owner);

            } else {
                console.log("ZERO RESULTS IN QUERY")
            }

        })

When you query a collection you get back an array of arrays.
This means when you do
let owner = results._owner
You won’t get any proper data to the owner variable.
try to log it with console.log(results._owner)
it will say that it doesn’t exists.
what you should do is loop trough the results.items
like this:


for (var i = 0; i < results.items.length; i++) {
 let owner = results.items[i]._owner;
 let date = results.items[i]._createdDate
 let items = results.items[i];

                userQuery(owner);
}

in the backend code evrything is fine except you have to name it a bit different again.

return wixData.query("Members/PrivateMembersData")
        .contains("_id", owner)
        .find()
        .then((userResults) => {

 if (userResults.items.length > 0) {

 let firstName = userResults.items[0].firstName;
 let email = userResults.items[0].loginEmail;
 let userId = userResults.items[0]._id;

 return console.log("query run with the following results: " + firstName, email, userId);

You won’t have to loop here since you only should have 1 output from the query,
thats why instead of looping i just putted a 0 as array number.

kind regards,
kristof.

Dear Kristhof,

Thank you very much for this.

So the initial query is running, retrieving the owner IDs from the content db. GREAT!

When running the backend function, error as follows:

The initial error couldn’t find

couldn't find 'wix-users'

So I imported

import wixUsersBackend from 'wix-users-backend';

Then the error gave the following in preview and live:

PREVIEW ERROR:

SyntaxError: Unexpected identifier

LIVE ERROR:

Uncaught (in promise) Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.

That being said, the code runs and the emails sent. I wont run it again (don’t want to spam my users). But I am confused.

Thank you for your help!

In your backend userQuery(owner) function, the following statement is incorrect:

return console.log("query run with the following results: " + firstName, email, userId);

Note: console.log() does not return anything. All it does is display the message in the browser’s console. Therefore, return console.log() does nothing except generate the error you are getting.

What you need to do is return the desired data. What you need is something like this:

return wixData.query("Members/PrivateMembersData")
.contains("_id", owner)
.find()
.then((userResults) => {
   if (userResults.items.length > 0) {
      let items = userResults.items;   // get array of returned items
      let item = items[0];             // only the first item is relevant
      return item;                     // return our item
   }
}   

The above code returns the entire item that was returned in the query. The calling code can then get the firstName, email, and userName from the returned item.