Send a return through a mongoose model - javascript

So I have this model :
'use strict';
var mongoose = require('mongoose');
var subscriberModel = function () {
var subscriberSchema = mongoose.Schema({
email: String
});
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
subscriberSchema.methods.validateSubscriber = function (subs, otherModel) {
var code = 0;
if (validateEmail(subs.email)) {
mongoose.model('Subscriber').findOne({'email': subs.email},
function(err, subscriber){
if (err) {
code = 2;
} else if (subscriber) {
code = 3;
} else {
subs.save(function(err){
if (err) {
code = 2;
}
code = 4;
});
}
});
} else {
code = 1;
}
};
return mongoose.model('Subscriber', subscriberSchema);
};
module.exports = new subscriberModel();
And I call it in another file that way :
var subscriberModel = require('../models/subscriber');
var subscriber = new subscriberModel({email: req.body.email.trim()});
var code = subscriber.validateSubscriber(subscriber, buyModel);
console.log('code = %s', code); // => displays "code = 0"
I guess it's a scope problem or something like that (because I don't use any callback -or maybe I should-) but I can't find nor the problem, nor the solution.
So, how can I get the returned code value ?
Thanks in advance.
[EDIT] : I found where the problem was and edited the code to explain it. So apparently my var is set in the callbacks of mongoose so now my question is; how do I wait for this callback to end as I need the result to display a message to the user in a webpage?

Okay so, I found the answer.
I edited my function validateSubscriber to send a callback: subscriberSchema.methods.validateSubscriber = function (subs, callback). And removed the otherModel occurences inside.
That way, when I call it in my other file I just have to do :
subscriber.validateSubscriber(subscriber, function(code){
buyModel.code = code;
console.log('code = %s', buyModel.code);
}
To get the code AND wait for the answer.
I hope it helped someone else.

Related

How to properly use Parse / Promise?

I am writing some JavaScript codes using Parse.com.
To be honest, I have been reading how to use Promise and done lots of research but cannot still figure out how to use it properly..
Here is a scenario:
I have two tables (objects) called Client and InvoiceHeader
Client can have multiple InvoiceHeaders.
InvoiceHeader has a column called "Amount" and I want a total amount of each client's InvoiceHeaders.
For example, if Client A has two InvoiceHeaders with amount 30 and 20 and Client B has got nothing, the result I want to see in tempArray is '50, 0'.
However, with the following codes, it looks like it's random. I mean sometimes the tempArray got '50, 50' or "50, 0". I suspect it is due to the wrong usage of Promise.
Please help me. I have been looking into the codes and stuck for a few days.
$(document).ready(function() {
var client = Parse.Object.extend("Client");
var query = new Parse.Query(client);
var tempArray = [];
query.find().then(function(objects) {
return objects;
}).then(function (objects) {
var promises = [];
var totalForHeader = 0;
objects.forEach(function(object) {
totalForHeader = 0;
var invoiceHeader = Parse.Object.extend('InvoiceHeader');
var queryForInvoiceHeader = new Parse.Query(invoiceHeader);
queryForInvoiceHeader.equalTo('headerClient', object);
var prom = queryForInvoiceHeader.find().then(function(headers) {
headers.forEach(function(header) {
totalForHeader += totalForHeader +
parseFloat(header.get('headerOutstandingAmount'));
});
tempArray.push(totalForHeader);
});
promises.push(prom);
});
return Parse.Promise.when.apply(Parse.Promise, promises);
}).then(function () {
// after all of above jobs are done, do something here...
});
} );
Assuming Parse.com's Promise class follows the A+ spec, and I understood which bits you wanted to end up where, this ought to work:
$(document).ready(function() {
var clientClass = Parse.Object.extend("Client");
var clientQuery = new Parse.Query(clientClass);
clientQuery.find().then(function(clients) {
var totalPromises = [];
clients.forEach(function(client) {
var invoiceHeaderClass = Parse.Object.extend('InvoiceHeader');
var invoiceHeaderQuery = new Parse.Query(invoiceHeaderClass);
invoiceHeaderQuery.equalTo('headerClient', client);
var totalPromise = invoiceHeaderQuery.find().then(function(invoiceHeaders) {
var totalForHeader = 0;
invoiceHeaders.forEach(function(invoiceHeader) {
totalForHeader += parseFloat(invoiceHeader.get('headerOutstandingAmount'));
});
return totalForHeader;
});
totalPromises.push(totalPromise);
});
return Parse.Promise.when(totalPromises);
}).then(function(totals) {
// here you can use the `totals` array.
});
});

angularJS / handing over parameter

In my AngularJS controller, I have the following code:
$scope.readMDB = function () {
var fs = require('fs');
var adodb = require('node-adodb');
var password = document.forms["mdbSelForm"]["pwd"].value;
var revSQL = fs.readFileSync('revenue.sql', 'utf-8');
revSQL = revSQL.replace(/(\r\n|\n|\r)/gm, " ");
revSQL = revSQL.replace(/[รค]/g, function () { return unescape("%E4") });
$scope.revenueSQL = String(revSQL);
console.log("revSQL: " + $scope.revenueSQL);
connection = adodb.open('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + $scope.selectedMDB + ';Jet OLEDB:Database Password=' + password + ';');
// debug
adodb.debug = true;
connection
.query($scope.revenueSQL)
.on('done', function (data) {
var revAll = JSON.stringify(data);
var revenueData = JSON.parse(revAll).records;
fs.writeFile('./model/revenue.json', JSON.stringify(data), function (err) {
if (err) throw err;
console.log("revenue.json saved");
});
return true;
})
.on('fail', function (data) {
return false;
});
}
A similar code worked fine while not using Angular. Now, it doesn't work no more, because of the
connection.query($scope.revenueSQL)
part. More precisely, the $scope.revenueSQL is not recognized as the SQL Statement that it actually is. Putting the SQL directly, without reading it from a file, works fine. Still, looking at my console, I see that $scope.revenueSQL is exactly what I want. But being put as parameter into .query(), something seems to go wrong. Any ideas?
Try
$scope.revenueSQL = $scope.$eval(String(revSQL));
to execute the expression on the current scope and returns the result.

Async Recursion with JavaScript and Node.js

This is probably a noob JavaScript question, but I'm looking to know if my solution to a problem I am having is 'correct'
I have created the following sample application that recreates my error:
Firstly in index.js
var processor = require('./fileProcessor/processor.js');
var container = {
source: "source.txt",
destination: "destination.txt"
};
new processor().process(container);
I create my container object which has the name of the source file and the name of the destination file. This is passed into the process function of the processor:
var fileProcessor = require('./fileProcessor.js');
module.exports = function Processor() {
this.process = function(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, this.process);
} else {
file.write(container, this.process);
}
}
};
};
As you can see this calls the read and write functions passing in the container and the process function as the callback, the fileProcessor looks like this:
var fs = require('fs');
module.exports = function() {
this.read = function(container, callback) {
fs.readFile(container.source, function (err, data) {
if(err) throw err;
container.body = data;
callback(container);
});
};
this.write = function(container, callback) {
fs.writeFile(container.destination, container.body, function(err) {
if(err) {
return console.log(err);
}
container.finished = true;
callback(container);
});
};
};
In simple terms the processor calls file.read, which reads the file and calls back into the process function, which then calls the write function. However at the end of the write function an error is thrown:
callback(container);
^
TypeError: object is not a function
Obviously when passing in this.process to file.write(container, this.process); the this isn't the this I intend it to be!
If I update my processor by adding a processFunction variable:
var fileProcessor = require('./fileProcessor.js');
module.exports = function Processor() {
var processFunction = function(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, processFunction);
} else {
file.write(container, processFunction);
}
}
};
this.process = function(container) {
processFunction(container);
};
};
Everything works fine. Is this a good way to do this or is there a better solution?
I think this is a fine way to do it. There is one possible modification that you might make. Since you are creating a new name in your scope just for the purpose of recursing, you could just name your function and refer to it by its name inside of the function.
module.exports = function Processor() {
this.process = function processFunction(container) {
var file = new fileProcessor();
if(container.finished === undefined) {
if(container.body === undefined) {
file.read(container, processFunction);
} else {
file.write(container, processFunction);
}
}
};
};
Then you can avoid creating a name (processFunction) that will be visible outside the function.
Take a look here for reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#Named_function_expression

error while using async in node.js

I am trying write a restapi using express framework and node.js. I am facing an error which I am unable to find out the root cause. I am getting the following error while trying to execute the code :
TypeError: Cannot read property 'node_type' of undefined where 'node_type' is a value that comes from a function
var GdbProcess = require('../../dao/gdb/processnds')
var mongo = require('mongodb');
var async = require('async');
exports.executeService = function(req,res){
//Make the process object to query
var manualProcessQuery = new Object();
manualProcessQuery.index = req.params.processmap;
manualProcessQuery.key = "pid";
manualProcessQuery.value = req.params.pid;
manualProcessQuery.event = req.params.event;
var tempDataNodeToExecute = new Object();
//This function returns an object (dataNodeToExecute) to execute
GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute){
if(err) res.send(err);
tempDataNodeToExecute = dataNodeToExecute;
var isSystem = false;
if (tempDataNodeToExecute.node_type =="system"){
isSystem = true;
}
var count = 0;
async.whilst(
function () { return isSystem },
function (callback) {
//execute the function
executeSystem(dataNodeToExecute,function(err,executionStatus){
if (err) callback(err);
count++;
if(executionStatus=="completed"){
manualProcessQuery.value = tempDataNodeToExecute.pid;
manualProcessQuery.event = "completed";
GdbProcess.getParametersbyNode(manualProcessQuery,function(err,dataNodeToExecute2){
if(err) callback(err);
tempDataNodeToExecute = dataNodeToExecute2;
if (tempDataNodeToExecute.node_type == "manual"){
isSystem = false;
}
});
callback();
}
});
},
function (err) {
if(err) res.send(err);
res.send("success");
}
);
});
}
var executeManual = function(prosNodeToExecute,callback){
//do something
callback (null);
}
var executeSystem = function(prosNodeToExecute,callback){
//do something
callback(null,"completed");
}
When I debug the code, i clearly see that node_type is available. Can someone help me to find the root problem here ?
remove the new object tempDataNodeToExecute and use dataNodeToExecute instead of it, and it is a good practice to check for null of an object before using its property so that the program does not crashes.

Returning results from a JavaScript function

I'm fairly new to JavaScript and am hoping someone can help me understand how to modify the function below so it will properly return a result when called. The code currently works and the handleResults function is called once the session string is generated. What I would like to do is modify the generateSessionString function so it will return the session string rather than passing it to handleResults. Can anyone give me suggestions on how I can accomplish this?
function generateSessionString(){
var cb = function (success, results){
if(!success)
alert(results);
if(results.code && results.message){
alert (results.message);
return;
}
handleResults(results);
};
var config = new KalturaConfiguration(gPartnerID);
config.serviceUrl = gServiceURL;
var client = new KalturaClient(config);
var partnerId = gPartnerID;
var userId = gUserName;
var password = gPassWord;
var expiry = gExpiry;
var privileges = gPrivileges;
var result = client.user.login(cb, partnerId, userId, password, expiry, privileges);
return result;
}
function handleResults(ks){
KalturaSessionString = ks;
}
if you like to write it in a sync way(it's still async code) you can try promise(in this example i used jQuery)
function generateSessionString(){
var dfd = new jQuery.Deferred();
var cb = function (success, results){
if(!success)
dfd.fail(results);
if(results.code && results.message){
dfd.fail (results.message);
return;
}
dfd.resolve(results);
};
var config = new KalturaConfiguration(gPartnerID);
config.serviceUrl = gServiceURL;
var client = new KalturaClient(config);
var partnerId = gPartnerID;
var userId = gUserName;
var password = gPassWord;
var expiry = gExpiry;
var privileges = gPrivileges;
client.user.login(cb, partnerId, userId, password, expiry, privileges);
return dfd.promise();
}
$.when(generateSessionString()).then(
function(session)
{
alert(session);
}
)
#Itay Kinnrot's answer is right.Actually,jQuery's on/trigger is another way to solve it,but $.Deferred is better.
if you want to know more about it,you could try to understand Pub/Sub Pattern.
This article is recommended:
http://www.elijahmanor.com/2013/03/angry-birds-of-javascript-blue-bird.html

Categories