gpt4 book ai didi

javascript - Mysql 和 node async.waterfall

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:49 26 4
gpt4 key购买 nike

我正在尝试查明该记录是否存在于我的数据库表中。如果那里不存在,那么我想将它添加到我的数据库中。我使用 Node 并使用 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);
};

编辑:

if 语句中的 return next() 行,我得到了建议的答案 TypeError: object is not a function 的错误>。在我的真实代码中,我实际上做了类似的事情(当我使用建议的答案时):

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);

});

我真的陷入了回调 hell ...

最佳答案

一个(未经测试的)版本应该做同样的事情,但在利用更多 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* 是更多方便。

相关文档:

关于javascript - Mysql 和 node async.waterfall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30642055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com