0
votes

I am having a heck of a time getting this simple functionality to work in Mobile Services. I followed an excellent post by Carlos Figuira on using expanded login scopes in mobile services and created a new shared script where my intent is to take the currently logged in user and retrieve their email address from the Google service. I first look in a Users table to see if they have a record in it. If not, I grab the info from the Google API and then store the information in the Users table. Ultimately, my function returns the email address. The reason for this is because we associate users to records by using the email address they use to log in to Google (we know their email addresses, not the funky Google id).

However, I'm receiving an error when calling the shared script. I'm not even sure the script itself is correct. I created a file named helpers.js in the shared folder of my git repository. Here's the shared script:

exports.getUserEmail = function (user) {
    return 'test';
    // The table object is the Users table for caching data.
    var table = request.service.tables.getTable(‘Users’);
    var cachedIdentities;
    var email = '';
    user.getIdentities({
        success: function (identities) {
            cachedIdentities = identities;

            table.where({
                userid: user.userid
            }).read({
                success: insertItemIfNotExists
            });

            function insertItemIfNotExists(existingItems) {
                if (existingItems.length > 0) {
                    // Return the email address stored in the users table:
                    email = existingItems[0].email;
                    return email;
                } else {
                    // Insert the user information into the Users table and return the email address:
                    var req = require('request');
                    if (cachedIdentities.google) {
                        var googleAccessToken = cachedIdentities.google.accessToken;
                        var url = 'https://www.googleapis.com/oauth2/v3/userinfo?access_token=' + googleAccessToken;
                        req(url, function (err, resp, body) {
                            if (err || resp.statusCode !== 200) {
                                console.error('Error sending data to Google API: ', err);
                                // TODO: send error feedback to calling script.
                                //request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
                            } else {
                                try {
                                    var userData = JSON.parse(body);
                                    var item = {
                                        userid: user.userid,
                                        username: userData.name,
                                        email: userData.email
                                    };
                                    // Set the email return value.
                                    email = item.email;
                                    // Cache our user data.
                                    table.insert(item, {
                                        success: function () {
                                            return email;
                                        },
                                        error: function (err) {
                                            console.error('Error inserting user Google data into Users table: ', err);
                                            return email;
                                        }
                                    });
                                } catch (ex) {
                                    console.error('Error parsing response from Google API: ', ex);
                                    // TODO: send error feedback to the calling script.
                                    //request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
                                }
                            }
                        });
                    } else {
                        // Return empty email.
                        console.log("Could not retrieve user information for '%d'", user.userid);
                    }
                }
            }

        }
    });
}

When I make a reference to the script from another script (Section.insert.js), I do this:

var helpers = require('../shared/helpers.js');

When this is called, I get the following error:

Error in script '/table/Section.insert.js'. SyntaxError: Unexpected token ILLEGAL [external code] at Object.exports.require (D:\home\site\wwwroot\App_Data\config\scripts\table__fxutil.js:9:12) [external code] at insert (:3:16) at :1:8

This seems to me as though it is a node.js error stating that the helpers.js module is not registered/installed.

Am I missing something? I added the helpers.js file via a git repository to Azure, and all of my changes have been showing up just fine.

Also, regarding the "getUserEmail" script in my helpers.js file, am I going about this the right way?

Thanks!

1
By the way, by enabling the Enhanced users feature on Azure Mobile Services, I was able to condense the "getUserEmail" function above down to this: // blogs.msdn.com/b/carlosfigueira/archive/2013/12/16/… exports.getUserEmailNew = function (user, callback) { user.getIdentities({ success: function (identities) { //console.log("Retrieved user email address as %j", identities.google.email); // Send back the email address: callback(identities.google.email); } }) } - hiro77

1 Answers

0
votes

Okay, I fixed the problem. In my shared script (getUserEmail), I referenced the Users table with backticks ( ` ) instead of single quotes somehow. The resulting error is one that node.js tends to throw if there are any badly formatted strings.