string is not a function
Hi,
I have a waterfall like this and get string is not a function error where i point in code. Thanks for your help.
async.waterfall([
function (callback) {
if (data.type === "create") {
var command = util.format('useradd %s -s /sbin/nologin -u %s -d /home/%s -g nginx -p%s', data.username, data.uid, data.username, data.password);
debug(command);
exec(command, callback);
} else {
callback();
}
},
function (callback) {
if (data.type === "create") {
var sql = util.format("insert into test (test1, test2, test3) VALUES ('test1', 'test2', 'test3');");
debug(sql)
debug('connected');
mysql.connect();
debug('I am sending the query');
mysql.query(sql, callback);
mysql.end();
debug('connection closed');
callback(); //<-- i get error in this line
} else {
callback();
}
},
function (callback) {
debug("mongodb");
collectionForServer.find({}, {}, function (err, docs) { callback(err, docs); });
},
function (servers, callback) {
debug("detail of servers");
for(var i = 0; i <= servers.length; i++){
debug(servers[i]);
}
callback();
},
function (callback) {
res.json({
'success': true,
'message': "",
'detail': req.body
});
}
],
function (error, output) {
debug('output: ' + output);
debug('error: ' + error);
if (error) {
res.json({
'success': false,
'message': "error"
});
}
});
After the first function in a waterfall, the other functions need to accept data as the first argument: https://github.com/caolan/async#waterfall
Change the second function from function(callback) to function(data, callback). Similarly, change all the next ones to function(data1, data2, ....dataN, callback)
@robludwig is right -- waterfall passes the results through the sequence.
async.series might fit your use case better.
After read your anwser, i changed my code like below but still i take same error
function (callback) {
if (data.type === "create") {
var command = util.format('useradd %s -s /sbin/nologin -u %s -d /home/%s -g nginx -p%s', data.username, data.uid, data.username, data.password);
debug(command);
exec(command, callback);
} else {
callback(null);
}
},
function (p1,callback) {
if (data.type === "create") {
var sql = util.format("insert into test (test1, test2, test3) VALUES ('test1', 'test2', 'test3');");
debug(sql)
debug('connected');
mysql.connect();
debug('I am sending the query');
mysql.query(sql, callback);
mysql.end();
debug('connection closed');
callback(null); //<-- i get error in this line
} else {
callback(null);
}
},
function (p1, callback) {
debug("mongodb");
collectionForServer.find({}, {}, function (err, docs) { callback(err, docs); });
},
function (servers, callback) {
debug("detail of servers");
for (var i = 0; i <= servers.length; i++) {
debug(servers[i]);
}
callback(null);
},
function (p1, callback) {
res.json({
'success': true,
'message': "",
'detail': req.body
});
}
],
function (error, output) {
debug('output: ' + output);
debug('error: ' + error);
if (error) {
res.json({
'success': false,
'message': "error"
});
}
});```