Tag: asynchronous

mysql用户authentication为nodeftpd

我想用mysqlftpdauthentication,但问题是connection.query()是asynchronous和脚本在mysql返回结果之前完成。 虽然,我已经制定了一个解决scheme,但它是一个黑客,并不是一个真正的解决scheme,是丑陋的。 那么如何在命令中使用mysql进行身份validation:通过nodeftpd库 所以它更多的是如何正确的,可能! conn.on('command:pass', function(pass, success, failure) { if(pass) con.query('select * from wp_users where user_login = ? and user_pass = ?',[username, pass], function(err, rows, fields){ if(rows.length > 0 ){ success(); return; }else{ failure(); return; } console.log('yy'); }); console.log('xx'); failure(); });

如何捕捉node.js中同步函数的错误?

在asynchronous函数中,我们可以简单地捕获callback中的错误。 例如 : asynchronous函数: fs.readdir(path, function(err){ //catch error ) 由于同步函数没有callback,我怎样才能捕捉错误? 同步function: fs.readdirSync(path); //throws some error 一种方法是使用try catch块: try{ fs.readdirSync(path); } catch(err){ //do whatever with error } 有没有其他方法可以做到这一点? 如果是,那哪一个更好?

并行运行asynchronous代码

使用aync库,是否可以创build真正的asynchronous任务,而无需使用预先构build的asynchronousI / O函数? 例如,如果运行此代码,它将始终按顺序运行这些函数,因为node.js是“单线程的”。 var async = require('async'); async.parallel([ function(){ console.log('1') }, function(){ console.log('2') }, function(){ console.log('3') }, function(){ console.log('4') } ]); 我们不应该阻止节点 – 是的 – 但node.js运行在单核上。 如果我们想利用其他内核,我们可以产生进程,并以这种方式使用asynchronous库。 除了集群模块之外,是否有其他核心上产生asynchronous进程的最佳实践方法?

更新待办事项列表,而无需刷新express nodejs应用程序中的页面

我是快速nodejs应用程序的新手。 我想做一个待办事项列表。 这是迄今为止的样子。 我的问题是当我添加一个待办事项,它需要一些时间来显示更改,因为它重新加载页面。 如何在不重新加载页面的情况下实时查看更改。 提前致谢。 在tasks.jade中 h1= title .list .item.add-task div.action form(action='/tasks', method='post', id='12345') input(type='hidden', value='true', name='all_done') input(type='hidden', value=locals._csrf, name='_csrf') input(type='submit', class='btn btn-success btn-xs', value='all done') form(action='/tasks', method='post') input(type='hidden', value=locals._csrf, name='_csrf') div.name input(type='text', name='name', placeholder='Add a new task') div.delete input.btn.btn-primary.btn-sm(type="submit", value='add') if (tasks.length === 0) | No tasks. each task, index in tasks .item […]

在嵌套的asynchronous模块函数中,“this”将如何正常运行?

我使用Sails.js框架和伟大的模型的function,我也使用它作为其依赖的async版本。 所以,为了解释一个实际的场景: 得到一个艺术家所唱的歌曲的所有评论 。 我应该首先查询歌曲,然后查询评论。 这就是为什么我使用async模块,特别是它的waterfallfunction。 但是,由于所有这些代码都放在一个Model文件中,而this文件是指模型本身,所以我有一个很大的疑问: 即使存在asynchronousfunction,这是否也会引用模型? 这是我正在做的一个代码示例: module.exports = { connection: 'seasonDB', attributes: { reachesMaxFavoriteTeam: function (team) { var results []; async.waterfall([ // Get favorite team function (cb) { var userTeam = this.userTeam; UserTeam.findOne({_id: userTeam}, function (err, foundUserTeam) { if (err) cb(err,null); var user = foundUserTeam.user; User.find({_id: user}, {_id:false, fanOf: true}, function (err, […]

节点asynchronous不运行asynchronous

我正在尝试使用async收集一些额外的数据到我的数组。 为此目的,我创造了以下内容: User_has_academy_module.findAll({ include: [{model: Module, include: [Category, ModuleType]}], where: {academy_team_id: team_id, user_id: user_id} }).then(function (modules) { var i = 0; async.map(modules, function (module,moduleCallback) { var act = Academy_Attempt.build(); if(module.dataValues.is_complete == 1){ act.findByUserTeamAndModule(module.dataValues.user_id, module.dataValues.academy_team_id, module.dataValues.module_id, function (result) { module.dataValues.academy_attempt = result; moduleCallback(null, module); }, function (error) { }) } }); onSuccess(modules); }) 你可以从上面看到我首先收集一个数组称为modules ,我需要循环每个这些modules我想findis_complete == […]

链接asynchronous函数Node.js bluebird mongoskin

我一直在阅读很多关于如何链接asynchronous函数的文章,但我似乎无法做到这一点! 正如标题所示。 我试图将mongoskin数据库调用链接在一起,以便我可以将所有信息以块的forms收集,然后最终将响应中的累积结果发送出去。 我有这样的对象用户: var User = { username: 'someusername', accounts: [{name: 'account_1'}, {name: 'account_2'}] } 对于每个帐户,我需要收集数据,然后在响应中发送累积的数据。 所以我使用以下for循环遍历帐户: var promise = require('bluebird'); var db = require('mongoskin').db('mongodb://localhost/someDB'); for(var x in user.accounts){ //Fetch account data user.accounts[x].accountData = fetchAccountData(user.accounts[x].name); } //Finally send the collected response response.send(user); 而fetchAccountData函数如下所示: function fetchAccountData(screen_id){ db.collection('master') .aggregate([ {$match: {screen_id: screen_id}} ], function(err, res){ if(err) […]

Javascript for循环等待callback

我有这个function: function tryStartLocalTrendsFetch(woeid) { var userIds = Object.keys(twitClientsMap); var isStarted = false; for (var i = 0; i < userIds.length; i++) { var userId = userIds[i]; var twitClientData = twitClientsMap[userId]; var isWoeidMatch = (woeid === twitClientData.woeid); if (isWoeidMatch) { startLocalTrendsFetch(woeid, twitClientData, function (err, data) { if (err) { // Couldn't start local trends fetch for […]

我应该在使用asynchronous队列时使用callback吗?

我正在使用async.js的queue ,我不确定是否应该一次处理一个任务。 任务本身是一个简单而快速的消息发送到套接字。 process: (task, done) -> task.client.send(task.model, done) VS process: (task, done) -> task.client.send(task.model) done()

在每次迭代期间迭代和调用Async的最佳方法

如果你不得不循环,并在我的情况下一堆调用存储库或网关,我该怎么做asynchronous意味着不包裹我的asynchronous调用内循环同步? 例如,什么是更好的方法(重构此代码)来循环一组ID,并调用find()如下我想要做的? 目标 :我想要一个ID数组,迭代它们,并且在每次迭代中,使用ID在我的网关上调用find()来获取该ID的对象,然后将其填充到最终数组中,当所有的说话和完成之后,我们会回来。 我在用什么 : q (承诺) 合伙 (打数据库) someModule.js var _gateway = require('./database/someGateway'); var cars = []; var car; for (var i = 0; i < results.docs.length; i++){ var carId = results.docs[i].carId; _gateway.find(carId) .then(function(data){ console.log('data[0]: ' + data[0].id); cars.push(data[0]); }) .done(); } console.log("cars: " + cars.length); // length here is 0 because my […]