I am using Parse.com javascript for CloudCode. I have little interest in becoming an expert in javascript, but I need to do some. I have worked out a scheme for modularizing (dividing into separate files) but it seems like there must be a better way. In my main.js
, I have code like this. The only function of main.js
is to call and link the various modules together.
var mods = {};
mods.functions = require('cloud/functions.js');
mods.user = require('cloud/user.js');
mods.functions.setMods(mods);
The mods variable collects references to each module. Then for each module that needs to call other modules, I call "setMods" in that module and pass mods to it. In setMods the module gets references to any other module it wants to access.
Each module then has code like this.
exports.setMods = function (mods) {
userMod = mods.user;
constants = mods.constants;
};
Parse.Cloud.define("getRecoveryQuestion", function(request, response)
{
var p0 = userMod.lookupUserByEmail(request.params.email);
var p1 = p0.then(function(user) {
// result is a User
// Now look for the password recovery question
if (user) {
// Found it
var question = user.get("pwdRecoveryQuestion");
response.success(question);
} else {
response.success(null);
}
return null;
});
});
In the User module, the exported function would look this way.
exports.lookupUserByEmail = function (email)
// returns null when not found
{
var User = Parse.Object.extend("User");
var query = new Parse.Query(User);
query.equalTo("email", email);
var p0 = query.first();
var p1 = p0.then(
function(result) {
return result;
}
);
return p1;
};
So, is there a better solution to this problem?