Tag: asynchronous

JS Lambda与async.parallel调用范围

我无法从其外部范围捕获一个variables到lambda函数。 当我运行这个代码时,它使用相同的variables执行。 我将这个variables传递给函数,但是我显然误解了lambda的作用域。 // Add tasks to async_tasks for(var i = 0; i < 10; i++){ var task = function(task_callback){ // I want to capture i and pass it to the function (function(i){ exports.defaultCarWithId(connection, i, function(err, data){ if (err) { console.log('error in query: ' + err.stack); fCallback("[Internal Server Error]", null); return; } task_callback(); }); })(i); […]

uncaughtException然后appendFile错误会导致问题? 的NodeJS

我很好奇,如果因为process.exit(1)是在asynchronous操作的callback,appendFile,如果原来的错误发生的地方将继续运行,并处于不稳定的状态。 我试图把process.exit(1)与fs内联,但不会写入。 更好的方法来创build一个错误logging,将login任何错误,将不胜感激。 。 const fs = require('fs'); process.on('uncaughtException', e1 => { fs.appendFile('./logs/error.log', e1.stack, e2 => { //if (e1 || e2) console.log(e1,e2); process.exit(1) }); })

asynchronous在Express中如何工作?

我在ExpressJS指南中find了以下内容: var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'dbuser', password : 's3kreee7' }); connection.connect(); connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) { if (err) throw err; console.log('The solution is: ', rows[0].solution); }); connection.end(); 这不应该是不好的做法吗? 我看到它的方式,可以执行查询之前连接结束。 这不会给错误吗?

node.js将值传递给asynchronous模式下的variables

我的问题是以下几点: 我有一个不错的文件夹抓取function,抓取文件的path。 我(我想)将这些文件用于testing目的。 1.)抓取文件2.)做一些testing3.)工作完成 这是我称之为的代码: walk(csvRoot, function(err, results){ if (err) throw err; for (var i = 0; i < results.length; i++) { return results[i] // – not working } }); 我的主要问题是,我真的想将结果传递给一个variables,它将包含这些path作为一个数组,但到目前为止没有运气。 该variables返回未定义,这就是我正在尝试解决目前。 你能告诉我该怎么做吗?

Node.js – > TypeError:无法读取上下文未定义的属性'then'

我有一个node.js文件调用一个asynchronous函数,我不断得到一个TypeError,其中“then”的属性不能在上下文中未定义。 async.js if ( typeof window === 'undefined' ) { require('../../app/async'); var expect = require('chai').expect; } describe('async behavior', function() { it('you should understand how to use promises to handle asynchronicity', function(done) { var flag = false; var finished = 0; var total = 2; function finish(_done) { if (++finished === total) { _done(); } } […]

asynchronous/等待错误在绑定函数中被吞噬

我正在运行使用async / await的Express应用程序。 我有两条看起来像这样的路线: app.post('/add-item', item.bind(null, 'add')) app.post('/remove-item', item.bind(null, 'remove')) 路由处理程序定义如下: async function item (action, req, res, next) { if (action === 'add') { var result = await addItemFromDB() res.json(result) } else { var result = await removeItemFromDB() res.json(result) } } 因为我想避免在try/catch包装addItemFromDB和removeItemFromDB函数,所以将其包装在助手函数asyncRequest : asyncRequest(async function item(req, res, next) { if (action === 'add') { var […]

async。<fn>在第一次迭代循环后限制停止

这个问题与我上一个问题的答案有关 。 有@robertklepbuild议我使用mapLimit()而不是.map()因为.map()不能处理大量的数据,并且解决scheme一切正常。 但是现在我重构了我的代码,现在在第一次循环迭代之后, .<fn>Limit()函数都没有运行。 我在这里错过了什么吗? var proccesBook = function(file, cb) { testFile(file, function (epub) { if (epub) { getEpuData(file, function (data) { insertBookInDB(data) }) }else{ cb(file) } }) } async.mapLimit(full_files_path, 10, proccesBook, function(err){ if(err){ console.log('Corrupted file', err); } else { console.log('Processing complete'); }; }) // —> only runs for the first 10 series data

只有刷新页面后,才能更新Firebase存储中的logging

在我的collections中修改Firebase中的logging并更改图片时,它将在Firebase和Google云端平台(存储)中成功更新,但在模板中我无法看到更改,除非我刷新页面。 我得到的错误是: Error: Can't set headers after they are sent. router.get('/edit/:id', function(req, res) { var id = req.params.id; firebase.database().ref(`collection/` + id).once('value') .then(function(data) { res.render('myEditTemplate', { id: id, collectionRecords: data.val() }); }) .catch(function(error) { res.render('error', { error: error }); }); }); router.post('/edit', upload.single('image'), function(req, res) { var id = req.body.id; var name = req.body.name; var image […]

如何正确使用node.js中的asynchronous函数的承诺?

我在节点中有一个读取文本文件的asynchronous函数,把整个东西放到一个string中,将string拆分成每一行,把它们放到一个数组中,随机返回一个。 在这里我已经实现了一个新的Promise函数来处理它: exports.readTextFileAndReturnRandomLine = function readTextFile(file) { //reads text file as string, splits on new line and inserts into array, returns random array element return new Promise((resolve, reject) => { var fs = require('fs'); var textFile = fs.readFile(file, 'utf8', (err, data) => { if (err) { return reject(err); } else { var array = data.toString().split("\n"); […]

如何强制for循环等待callback完成在nodejs?

entry.find(function(err,user){ for(var i=0;i<user.length;i++){ if(user[i].tablenumber==-1){ assigntable(user[i]); } } }); 所以我在这里要做的是在调用asynchronousassigntable函数之前等待每个for循环完成。 Assigntable是对数据库进行更改的callback。 我现在的问题是,因为asscomntable的callback是在for循环完成后调用的,所有的用户都会被分配到表1中。但是我想要的是:将第一个分配给表1 – >在下一个循环中分配表1被分配 – >用户2被分配到表2等等。 我怎么做? 编辑:Assgintable是一个recursion调用,在callback中编辑数据库。 更新将基于从前一个循环产生的数据库的结果,但我没有在这里相关。 function assigntable(user,tableindex){ console.log("in recursion with tableindex"+tableindex); if(tableindex==3) return; console.log(user.date+user.time+tableindex); entry.find({ $and: [ { "tablenumber": tableindex}, { "date": user.date }, {"time":user.time} ] },function(err, result) { if(result.length==0){ console.log(result+"result is "); entry.update({"_id":user._id},{"tablenumber":tableindex},function(err, numberAffected) { if(!numberAffected){ } else { console.log("entryupdated with […]