async.waterfall() breaks on passing callback to bcrypt.hash() - node.js

I am new to async/await and have set up a basic node.js server that handles form data for user registration. Below is my code
async.waterfall([async function(callback){ //Method 1
const hash = await bcrypt.hash(password, 10/*, () => { //breakpoint
console.log("Hash Generated Successfully");
}*/);
return hash;
}, function(hash, callback){ //Method 2
console.log(`The value of passed arg is: ${hash}`);
callback(null, 'success');
}], function(err, result){
if(err){
throw err
}
else {
console.log(result);
}
});
In Method 1, if i don't provide the callback to bcrypt.hash(), the code works correctly and the value of hash is printed. However, if i do provide the callback, i get this output:
The value of passed arg is: undefined.
So, i have two questions here.
1) Why does async.waterfall() break on providing callback to bcrypt.hash()?
2) What is the other way to do error handling, other than callbacks?

Passing the requisite parameters to the bcrypt callback function is a necessity if you plan on including the anonymous function as a parameter. For ex:
const hash = await bcrypt.hash(password, 10, (err, hash) => { // Added err, hash params.
console.log("Hash Generated Successfully");
});
return hash;

Related

AWS Lambda query with async waterfall in node.js 8.10

I send two query sequentially
Query the data from A tables, and then accoring to the result, query the data from B table.
So, I query the data like that,
var async = require('async');
var mysql = require('mysql');
var config = require('./config.json');
var connection = mysql.createConnection({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
exports.handler = (event, context, callback) => {
// TODO implement
var tasks = [
function (callback) {
connection.query("SELECT email FROM Visitor WHERE id =?;", [1], function (err, row) {
if (err) return callback(err);
if (row.length == 0) return callback('No Result Error');
callback(null, row[0]);
})
},
function (data, callback) {
connection.query("SELECT id,signtime FROM Board WHERE email =?;", data.email, function (err, row) {
if (err) return callback(err);
if (row.length == 0) {
return callback('No Result Error');
}else {
callback(null, row[0])
}
})
}
];
async.waterfall(tasks, function (err, result) {
if (err)
console.log('err');
else
***return result;***
console.log('done');
connection.end();
});
};
I log the data with console.log(), it take the data in command line.
But in lambda, put the function into exports.handler, it response null.
If I change the 'return result' to callback(result), it occurs error.
I think it maybe too simple to solve this problem
If you know about that, please help me
In the first case, response is null because you didn't use neither Promise, nor callback to let the Lambda sandbox know that the job is done. In the second case, you used the callback, but you passed the result as the first argument to it. Lambda programming model for Node.js follows a principle called "error first callback". Long story short, if any error occurred during execution, you should go with callback(error), and if everything is ok and you need to return some result from lambda, you should go with callback(null, result). So basically on your line before console.log('done'); use callback(null, result) and it will work for you.

This object is empty inside callback function of run() method

I'm writing a server using NodeJS, Express and SQLite in order to provice RESTful services.
I want to create an endpoint for adding a new user to the database. The endpoint receives a POST request with the parameters username and password, and responds with the id of the newly created user.
According to the node-sqlite3 package documentation, when the run method succeeds, the this object inside the callback function contains the properties lastID and changes.
My problem is that, even though the run method succeeds and the new user is successfully inserted into the database, the this object is empty.
What am I doing wrong? How can I access this.lastID?
app.post('/users', (req, res) => {
const {
username,
password
} = req.body;
db.get(
`SELECT id
FROM Users
WHERE username = ?`,
[username],
(err, row) => {
if (err) {
console.error(err);
return res.sendStatus(500);
} else if (row) {
return res.status(400).send('Username already exist.');
}
db.run(
`INSERT INTO Users (username, password)
VALUES (?, ?)`,
[username, password],
(err) => {
if (err) {
console.error(err);
return res.sendStatus(500);
}
console.log(this); // prints {} to the console
res.send(this.lastID); // response body is empty
}
);
}
);
});
Try using an old-school function instead of an arrow function for your callback. I think with arrow functions this cannot be bound to a value.

Make Node.js code synchronous in Mongoose while iterating

I am learning Node.js; due to asynchronous of Node.js I am facing an issue:
domain.User.find({userName: new RegExp(findtext, 'i')}).sort('-created').skip(skip).limit(limit)
.exec(function(err, result) {
for(var i=0;i<result.length;i++){
console.log("result is ",result[i].id);
var camera=null;
domain.Cameras.count({"userId": result[i].id}, function (err, cameraCount) {
if(result.length-1==i){
configurationHolder.ResponseUtil.responseHandler(res, result, "User List ", false, 200);
}
})
}
})
I want to use result in Cameras callback but it is empty array here, so is there anyway to get it?
And this code is asynchronous, is it possible if we make a complete function synchronous?
#jmingov is right. You should make use of the async module to execute parallel requests to get the counts for each user returned in the User.find query.
Here's a flow for demonstration:
var Async = require('async'); //At the top of your js file.
domain.User.find({userName: new RegExp(findtext, 'i')}).sort('-created').skip(skip).limit(limit)
.exec(function(err, result) {
var cameraCountFunctions = [];
result.forEach(function(user) {
if (user && user.id)
{
console.log("result is ", user.id);
var camera=null; //What is this for?
cameraCountFunctions.push( function(callback) {
domain.Cameras.count({"userId": user.id}, function (err, cameraCount) {
if (err) return callback(err);
callback(null, cameraCount);
});
});
}
})
Async.parallel(cameraCountFunctions, function (err, cameraCounts) {
console.log(err, cameraCounts);
//CameraCounts is an array with the counts for each user.
//Evaluate and return the results here.
});
});
Try to do async programing allways when doing node.js, this is a must. Or youll end with big performance problems.
Check this module: https://github.com/caolan/async it can help.
Here is the trouble in your code:
domain.Cameras.count({
"userId": result[i].id
}, function(err, cameraCount) {
// the fn() used in the callback has 'cameraCount' as argument so
// mongoose will store the results there.
if (cameraCount.length - 1 == i) { // here is the problem
// result isnt there it should be named 'cameraCount'
configurationHolder.ResponseUtil.responseHandler(res, cameraCount, "User List ", false, 200);
}
});

Unknown error when using bcrypt.hash

I have this pre mongoose middleware for saving passwords,I previously used synchronous implementation,now I am doing an asynchronous implemntation as mongoose middleware:
schema.pre('save', function(next) {
var user = this;
var SALT_WORK_FACTOR = 5;
if (!user.isModified('local.password')) return next();
bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.local.password, salt, function(err, hash) {
if (err) return next(err);
user.local.password = hash;
next();
});
});
});
The code just throws an unknown error when it gets to bcrypt.hash although the error is previously null. If I use something like stackup,the error looks like this:
E:\Do\login\node_modules\stackup\index.js:32
error.stack = activeTrace.toString(error.stack);
^
TypeError: Cannot assign to read only property 'stack' of No callback function w
as given.
at AsyncListener.error (E:\Do\login\node_modules\stackup\index.js:32:19)
at asyncCatcher (E:\Do\login\node_modules\stackup\node_modules\async-listene
r\glue.js:123:26)
at process._asyncFatalException [as _fatalException] (E:\Do\login\node_modul
es\stackup\node_modules\async-listener\glue.js:211:14)
You're using bcrypt-nodejs, which expects two callbacks:
hash(data, salt, progress, cb)
docs
You've only provided it once, and so cb isn't defined when bcrypt-nodejs hits it.

How to add an array of objects to parse.com db?

I'm developing an application and need to add many items at a time.
How can I do that with node.js?
This is the npm module for parse.com but there is no method like
insertAll("Foo", [objs...], ...)
I don't want to insert single object every time.
Write a convenience function that interfaces between your application and parse.com. You will have to write the iteration code once (or debug mine)
var async = require('async');
var parseApp = require('node-parse-api').Parse;
var APP_ID = "";
var MASTER_KEY = "";
var parseApp = new Parse(APP_ID, MASTER_KEY);
function insertAll(class, objs, callback){
// create an iterator function(obj,done) that will insert the object
// with an appropriate group and call done() upon completion.
var insertOne =
( function(class){
return function(obj, done){
parseApp.insert(class, obj, function (err, response) {
if(err){ return done(err); }
// maybe do other stuff here before calling done?
var res = JSON.parse(response);
if(!res.objectId){ return done('No object id') };
done(null, res.objectId);
});
};
} )(class);
// async.map calls insertOne with each obj in objs. the callback is executed
// once every iterator function has called back `done(null,data)` or any one
// has called back `done(err)`. use async.mapLimit if throttling is needed
async.map(objs, insertOne, function(err, mapOutput){
// complete
if(err){ return callback(err) };
// no errors
var objectIds = mapOutput;
callback(null, objectIds);
});
};
// Once you've written this and made the function accessible to your other code,
// you only need this outer interface.
insertAll('Foo', [{a:'b'}, {a:'d'}], function(err, ids){
if(err){
console.log('Error inserting all the Foos');
console.log(err);
} else {
console.log('Success!);
};
});

Resources