我试图找出我的数据库表中是否存在记录。如果那里不存在,那么我想将它添加到我的数据库中。我使用节点并使用async.waterfall
。但是我的代码出了问题,我找不到什么。它不会将记录添加到我的数据库中。有什么建议吗?
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'database'
});
for (var m = 0; m <= (urls.length-1); m++) {
var Myurl = urls[m];
var ThisName= TheNames[m];
//IIFE function. I omitted the error handling for clarity.
(function(Myurl,ThisName){
async.waterfall([
//First find out if the record exists in mydata
function(next){
connection.query('SELECT * FROM mydata WHERE UrlLink=?
LIMIT 1',[Myurl],next)
},
//If the record does not exist, put it in mydata
function(results,next){
if (results.length==false){
console.log("New Thing!");
//Do some stuff here, request(Myurl...) to find TableName.
var post = {UniqueUrl:Myurl,ThingName:ThisName,TheName:TableName};
connection.query('Insert INTO mydata Set ?', post,next);
};
};
],
//Final callback
function(err, results) {
connection.end();
};
);
})(Myurl,ThisName);
};
EDIT
:
我在TypeError: object is not a function
的{{1}}行找到了回答return next()
的错误。在我的真实代码中,我实际上做了类似的事情(当我使用建议的答案时):
if statement
我真的陷入async.series([
//First function
function(callback){
//Some calculations...
callback();
},
//Next function
function(callback){
var i = 0;
//Loop
async.whilst(
function() { return i <= thefooz.length-1; },
//The innerCallback:
function(innerCallback){
//Some calculations where i get urls and TheNames.
async.forEachOf(urls, function(Myurl, m, eachDone) {
var ThisName = TheNames[m];
async.waterfall([
function(next) {
connection.query(
'SELECT * FROM mydata WHERE UrlLink=? LIMIT 1',
[ Myurl ],
next
);
},
function(results, next) {
if (results.length !== 0) {
return next();
}
console.log("New Thing!");
//Do some stuff here, request(Myurl...) to find TableName.
var post = {
UniqueUrl : Myurl,
ThingName : ThisName,
TheName : TableName
};
connection.query('Insert INTO mydata Set ?', post, next);
}
], eachDone);
}, function(err) {
if (err) throw err; // or however you like to handle errors
connection.end();
});
setTimeout(function() { i++; innerCallback(); }, 10000);
}); //close my calculations in innerCallback.
}, //close innerCallback.
); //close asyns.whilst
callback;
} //close function(callback)
], function(error){
if (error) return next(error);
});
......
答案 0 :(得分:3)
一个(未经测试的)版本应该做同样的事情,但在利用async
的更多优点方面有一些错误修正和增强功能:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'user',
password : 'password',
database : 'database'
});
// Process all url's in parallel (at the same time). If you to process them
// sequentially (one after another), you can use `async.forEachOfSeries()`
// instead. Or, if you want to limit the number of parallel requests, use
// `async.forEachOfLimit()`.
async.forEachOf(urls, function(Myurl, m, eachDone) {
var ThisName = TheNames[m];
async.waterfall([
function(next) {
connection.query(
'SELECT * FROM mydata WHERE UrlLink=? LIMIT 1',
[ Myurl ],
next
);
},
function(results, fields, next) {
if (results.length !== 0) {
return next();
}
console.log("New Thing!");
//Do some stuff here, request(Myurl...) to find TableName.
var post = {
UniqueUrl : Myurl,
ThingName : ThisName,
TheName : TableName
};
connection.query('Insert INTO mydata Set ?', post, next);
}
], eachDone);
}, function(err) {
if (err) throw err; // or however you like to handle errors
connection.end();
});
您也可以使用forEach*
,而不是使用each*
,但由于您需要数组索引forEach*
更方便。
相关文件: