Tag: 瀑布

查询数据库时出现asynchronous瀑布问题

我构build了一组对象从csvstream保存在mongoDB中。 对于每个csv行,我需要在保存之前validation一个或多个对象在MongoDB中不存在。 下面的代码在GETpath中运行。 我一直在尝试使用asynchronous瀑布,但它不像我预期的那样。 这是代码 async.waterfall([ function (callback) { console.log('in function 1'); –> Diagnosis.findOne({name: diagnosisName}, function (doc){ console.log(JSON.stringify(doc)) }) callback(null); }, function (callback) { console.log('in function2') callback(null) }], function(err, results) { console.log('finished!') res.send("complete"); }) 我希望这会返回以下内容 在function1中 doc对象在JSON中 在函数2中 完了! 而是我得到 在函数1中 在函数2中 完了! 空值 只要没有findOne()调用,它就按预期运行。 我在想什么? 非常感激

最好的方式使用循环和aysnc水落在一起

我是Nodejs的新手,需要一些编写更好的代码的指导。 这是我的问题。 我有一个function,我正在使用asynchronous降水模型。 我想在一个循环中调用这个函数,如果在中间出错了,则在循环结束时通知一些结果。 但由于某种原因,我收到了一个未定义的回应。 function myFunc (arg1) { async.waterfall( [ function() { //do something callback(null, data); }, function(data, callback) { //do something callback(null, 'done'); } ], function(err, result) { return {"error" : err, "res" : result}; } } //Here I am calling my function for (var d in mydata) { var retdata = myFunc […]

节点asynchronous瀑布callback已被调用

我试图读取从asir.waterfall目录的一些文件,在我看来,我正在做的东西正确的,但我得到了指定的错误和readData函数从来没有被调用。 怎么了? var fs = require("fs"); var async = require("async"); var folder = "./files/"; try { async.waterfall([ function readDir(cb) { fs.readdir(folder, function(err, files) { cb(err, files); }); }, function loopFiles(files, cb) { files.forEach(function(fn) { console.log("loop " + fn); cb(null, fn); }); }, function check(fn, cb) { console.log("check "+fn); fs.stat(folder + fn, function(err, stats) { console.log(stats.isFile()); […]

在Async瀑布中调用最后的callback有没有什么坏处?

async.waterfall( [ function(cb){ //do some stuff and then never call cb leaving the async waterfall unfinished },…], function(err,result){ //we only get here if the cb is called above and it never is } ) 这有什么伤害吗? 我意识到它不是按照devise的方法使用的,但是我只是发现了一堆我正在维护的代码(由不再被大声呼喊的人编写),而且我担心这可能会让事情陷入困境。 如果这是在一个服务器上运行的重击,会造成一个问题?

node.js中的async.js瀑布:如何使用bind和这个?

我正在学习来自具有有限JavaScript级别的PHP背景的node.js。 我想我现在已经解决了asynchronous方法所隐含的思维方式的变化。 我喜欢它。 但是,和我之前的很多人一样,我很快理解了“厄运金字塔”的具体含义。 所以我build立这些小的“虚拟”路线和视图,以了解如何正确使用Async.js。 我只花了最后5个小时写下面的代码(当然重写了几十次)。 它的工作原理,但我想知道如何进一步,使这个代码更简单(不详细,更容易阅读和维护)。 我在网上发现了很多资源,特别是在这里,但总是通过一些信息在这里和那里。 我猜在这一点上,我应该使用“绑定”和“this”与async.apply使缩短瀑布调用的最后2个函数。 问题是获取对象“db”定义,所以我可以使用它的“收集”方法(第二个function)。 我真的search了一个谷歌的例子,但是你不能直截了当地find“asynchronous瀑布绑定”(以及我试过的许多关键字变体)的例子。 当然有答案,但似乎没有什么与这个特殊问题有关…矿石,很可能,我还没有理解他们。 有人可以帮我吗? 我会很感激。 app.get('/dummy', function(req, res) { var MongoClient = require('mongodb').MongoClient; async.waterfall( [ async.apply(MongoClient.connect, 'mongodb://localhost:27017/mybdd'), function(db, callback) { db.collection('myCollection', callback); }, function(collection, callback) { collection.find().sort({"key":-1}).limit(10).toArray(callback); } ], function(err, results) { if (err) console.log('Error :', err); else { res.render('dummy.jade', { title:'dummy', results: results} ); } […]

在expressjsasynchronous瀑布处理错误

我不明白为什么expressjs在抛出async.waterfall时不处理错误 var express = require('express') , app = express.createServer() , async = require('async'); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); app.get('/error', function(req, res){ throw Error('Aie'); }); app.get('/asyncerror', function(req, res){ var that = this; async.waterfall([ function(next){ console.log('1'); next("42", "2"); }, function(arg, next) { console.log(arg); res.json('ok'); } ], function(err){ console.log(this); throw Error('Aie'); }); }); app.listen(8888, function(){ console.log('Listen on […]

在瀑布内节点asynchronous并行

我在Nodejs中构build一个服务器来从某个数据库检索数据。 我已经使用了asynchronous库了一段时间了,并想出了一些东西,例如把一个瀑布放在一个并行函数中。 我偶然发现了一个问题,我首先需要执行一个查询,然后在可以同时执行的其他查询中使用该查询的结果。 代码看起来像这样: async.waterfall([ function(callback) { connection.query( query, function(err, rows, fields) { if (!err) { callback(null,rows); } else { callback(null,"SORRY"); } } ); }, async.parallel([ function(resultFromWaterfall,callback) { connection.query(query, function(err, rows, fields) { if (!err) { callback(null,rows); } else { callback(null,"SORRY"); } } ); }, function(resultFromWaterfall,callback) { connection.query(query, function(err, rows, fields) { if (!err) { […]

什么是async.waterfall的简单实现?

我正在使用asynchronous库中的一些函数,并且要确保我理解他们在内部是如何做的。 然而,我卡在async.waterfall ( 这里的实现 )。 实际的实现使用了库内的其他function,没有太多的经验,我觉得很难遵循。 有人可以不担心优化,提供一个非常简单的实现,实现瀑布的function? 大概是这个答案可比的东西。 从文档中 ,瀑布的描述: 按顺序运行函数的任务数组,每个函数将结果传递给数组中的下一个。 但是,如果任何任务将错误传递给自己的callback,则不执行下一个函数,并立即调用主callback并返回错误。 一个例子: async.waterfall([ function(callback) { callback(null, 'one', 'two'); }, function(arg1, arg2, callback) { // arg1 now equals 'one' and arg2 now equals 'two' callback(null, 'three'); }, function(arg1, callback) { // arg1 now equals 'three' callback(null, 'done'); } ], function (err, result) { // result […]

了解Node.JS使用async.waterfall如何执行外部函数

我需要使用async.js模块执行asynchronous函数。 但是当我执行外部函数时,我遇到了一些问题。 代码通过。 但是当我更改全局variables为本地variables,我不能设置使用参数。 var async = require('async'); var ogs = require('open-graph-scraper'); // global variables var param1 = {url: 'http://www.google.com/'}; var param2 = {url: 'https://www.yahoo.com/'}; function function1(callback){ ogs(param1, function(error, data1) { callback(null, data1); }); } function function2(data1, callback){ ogs(param2, function(error, data2) { callback(null, data1, data2); }); } function function3(data1, data2, callback){ console.log(data1); console.log("—————"); console.log(data2); } […]

Asyncjs:绕过瀑布链中的函数

我想用asyncjs中的nodejs从一个瀑布函数链中跳过一个函数。 我的代码如下所示: async.waterfall([ function(next){ if(myBool){ next(null); }else{ // Bypass the 2nd function } }, // I want to bypass this method if myBool is false in the 1st function function(next){ }, // Always called function(next){ } ]); 你知道一个正确的方法做这个没有放: if(!myBool){ return next(); } 在我想绕过的function。 谢谢 !