Tag: asynchronous

Mongoose不会在Mocha中正确删除数据库和closures连接

我有两个简单的testing,但其中一个testing通过,但不是另一个,因为架构再次编译。 OverwriteModelError:编译后无法覆盖CheckStaging模型。 这是我的一个testing,因为它正在运行第一。 var mongoose = require('mongoose'), StagingManager = require('../lib/staging_manager'), expect = require('expect.js'); describe('Staging manager', function() { var StagingModel; beforeEach(function(done) { mongoose.connect('mongodb://localhost/BTest'); StagingModel = new StagingManager(mongoose).getStaging(); done(); }); describe('find one', function() { it('should insert to database', function(done) { // Do some test which works fine }); }); afterEach(function (done) { mongoose.connection.db.dropDatabase(function () { mongoose.connection.close(function () […]

async.mapLimit用法来发出多个请求

我有这样的路线: async.mapLimit(urls, 10, getCertificatesFromPage, callback) 其中,urls是一个包含url的数组,getCertificatesFromPage: getCertificatesFromPage = (url, callback) -> request url, (err, res, html) -> $ = cheerio.load(html) allRows = $('div.search-result-shop') objCollector = [] allRows.each () -> links = $(this).find('a') obj = companyName: $(this).find('.companyname').text() pageDetailsUrl: '' link: '' for link in links extractedLink = link.attribs.href if extractedLink.includes('http') obj.link = extractedLink else obj.pageDetailsUrl = […]

在内部forEach循环中集成asynchronousmongo调用

我有两个循环,用户的外部循环和内部的循环遍历每个用户的场所ID。 在内部循环中,我想查找场地并将其附加到外观( userItem )中定义的数组。 但是,因为forEach是同步的,并且mongo数据库查找是asynchronous的,所以结果始终为空。 我试图整合这个答案,但无济于事。 这个怎么做? ret = []; users.forEach(function(user) { var userItem = user.getSanitised('ADM'); userItem.venues = []; var tmp = []; userItem.adminVenueIds.forEach(function(adminVenueId){ tmp.push(function(callback) { Venue.findOne({_id:adminVenueId}, function(error, venue) { callback(null, venue.toObject()); }); }); }); async.parallel(userItem.venues, function(err, result) { /* this code will run after all calls finished the job or when any of the […]

查询数据库时出现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()调用,它就按预期运行。 我在想什么? 非常感激

Bluebird的asynchronous承诺处理

出于某种原因,我得到了一个蓝鸟许诺,我附加处理程序asynchronous。 例如: var promise = Promise.reject(new Error('Handled rejection')); setImmediate(function () { promise.then(console.log, console.error); }); 虽然我的承诺处理得很好,但蓝鸟却警告我一个未经处理的拒绝。 我可以通过同步添加一个空的拒绝处理程序来欺骗它: promise.catch(function () {}); 但是,这看起来真的很黑。 有没有适当的方法来处理这种情况? 编辑 :在回答这个评论 ,我发布了一个更详细的例子,在这个Gist我真正的用例。 它还包含我在等待Bluebird 3.0发行版时使用的解决方法。 正如Benjamin所解释的那样,Bluebird 3.0将带来一个解决scheme.suppressUnhandledRejection() 。

Node.js杀死一个asynchronous调用

有什么办法在Node.js杀死asynchronous操作,并阻止它完成(调用callback,或解决承诺)? 更具体的说,我做了一个http.get()调用。 这是一个asynchronous操作,最终会调用通过的callback,或者触发一个错误事件。 但是这个过程需要很长时间,我的代码中的逻辑可能会说:“嘿,我已经等了这么久了,我不想再等了!” 并做一些其他的事情。 问题是,callback最终会被调用,这可能会搞砸的东西! 所以是的,有没有办法阻止这个callback被执行? 我的想法是: var notWaitingAnyMore = false; http.get({}, function(res) { if(notWaitingAnyMore) { // do stuff! } }); //do some other stuff //now fed up with waiting: notWaitingAnyMore = true; 但是,这似乎不是一个很好的解决scheme,我…

learnyounode#9杂耍asynchronous,可以正式解决吗?

所以我现在正在学习node.js,并且之前做了一些多任务处理,而我的概念是asynchronous和多任务处理有许多类似的isses,这就引出了我的问题。 这个问题的官方解决scheme是: var http = require('http') var bl = require('bl') var results = [] var count = 0 function printResults () { for (var i = 0; i < 3; i++) console.log(results[i]) } function httpGet (index) { http.get(process.argv[2 + index], function (response) { response.pipe(bl(function (err, data) { if (err) return console.error(err) results[index] = data.toString() //AREA […]

在nodeJS中的asynchronous函数内返回variables

我有一个nodeJS和他们的asynchronous函数有点问题。 我需要一个GET请求获取一些API数据,然后提交一些数据返回到2个variables函数调用进一步使用的函数。 但问题是,我不能使用asynchronous请求函数外的响应数据来返回一些数据。 有没有可能意识到这一点? 如果不是,我该怎么做呢? var geoData = function(address){ // Google API Key apikey = 'XXX'; // google API URL for geocoding var urlText = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address)+'&key=' + apikey; request(urlText, function (error, response, body) { if (!error && response.statusCode == 200) jsonGeo = JSON.parse(body); console.log(jsonGeo.results[0].geometry.location); } }) // Variable jsonGeo isn't declared here latitude […]

Nodejs:mysql:只能查询最多两次

我为我的nodejs项目使用mysql 。 这是我的SQL代码: var mysql = require('mysql'); var config = { host: 'localhost', username: 'root', password: 'root', port: 8889, database: "BookDB", connectionLimit: 100, charset: 'UTF8_GENERAL_CI', dialect: "mysql" }; var pool = mysql.createPool({ connectionLimit: config.connectionLimit, host : config.host, user : config.username, password : config.password, database : config.database, port : config.port, charset : config.charset, dialect : config.dialect […]

Node.js中的asynchronous代码和循环?

我已经在asynchronous编程方面做了好几个小时的研究,但我似乎无法在Node中掌握这个单一的概念,所以我想知道这里有人能帮助我。 我已经写了下面的代码示例来返回/输出一个简单的string,这是一个对象string的串联: var itemCollection = { item1 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }], item2 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }], item3 : [{ foo : "bar" }, { foo : "bar" }, { foo : […]