Tag: asynchronous

meteor方法不起作用

假设我有一个叫Tasks的Collection ,它里面几乎没有任务。 我调用一个方法来返回一个任务数组给用户,但由于某种原因,它不会返回任何东西。 这里是一个代码例如: if (Meteor.isClient) { // This code only runs on the client Template.body.helpers({ tasks: function () { // Show newest tasks first Meteor.call("getTasks", function(error, result) { return result; // Doesn't do anything.. }); } }); } Meteor.methods({ getTasks: function() { return Tasks.find({}, {sort: {createdAt: -1}}); } }); 任何想法,为什么当我打电话的方法,它不会返回任何东西?

创build一个键值对(从mongoDB数据库中获取)的数组

我正在尝试以dynamic的方式创build一个key-value对的数组。 values部分是从我的routes数据库中获取的。 这就是我正在做的。 router.get('/account',isAuthenticated, function(req,res){ var following_users_details = []; for (var i = 0;i<req.user.following_users.length;i++) { var following_user = req.user.following_users[i];// following_users is a field of Array type in my MongoDB database User.findOne({'username' : following_user }, function(err,user) { following_users_details.push({ username: user.username, profilepic : user.photo, userbio : user.bio }); }); } res.render('users/userAccount',{user:req.user, following_users_details:following_users_details }); }); 但是当我尝试打印我的key-value对的数组时,我什么也得不到。 for(var i […]

运行蓝鸟承诺顺序,没有返回值?

这个问题已经以各种方式提出,但不是那么简单。 这个Promise.all将被重写,以便promise1在promise2之前完全运行? var promise1 = function() { .. lots of promise stuff }; var promise2 = function() { .. lots more promise stuff }; Promise.all([promise1, promise2]).then(function() { log.info("ran promise1 & promise2"); }); Promise.all并行运行promise1和promise2。

for循环中的async.waterfall转义for循环

在POSTtypes的Form Action中,我们获取Node.JS/Express所有值, Node.JS/Express其保存到MongoDB 。 一个隐藏的字段决定了前端JavaScript的一个属性的长度,它的值被更新为隐藏字段的值。 这个长度被用在后端(Node)中以迭代项目列表。 我有一个async.waterfall函数和一个for loop里面运行这样的。 async.waterfall([ function(callback){ var itemLength = req.body.itemLength; var itemProp,itemComponent; var destination; var destinationsArray =[]; for(var k=1; k<=itemLength; k++){ destination = new Destination({ name: req.body['destinationName'+k], }); itemComponent = { "itemCompProp" : req.body['itemCompProp'+k] }; itemProp = new ItemProp({ itemComponent: itemComponent }); itemProp.save(function(err,itemPropSaved){ destination.newProperty = itemPropSaved._id destination.save(function(err,destinationSaved){ if(err){ console.log("Error== " + […]

node.js与async.each async.series不按预期方式执行

我真的很努力获得以特定顺序迭代的数组,并且每个应该在下一个可以开始之前完成。 我试过使用async.series和async.each,但结果并不像我预期的那样。 我有这个代码: var async = require('async'); var abc = ['a', 'b', 'c']; // Step 1 function one(callback) { async.each(abc, function(issue, callback) { setTimeout(function(){ issue += '1'; console.log('Processing issue ' + issue); }, 2000); callback(); }, function(err) { if(err) { console.log('Fail 1'); } else { console.log('Success 1'); } }); } // Step 2 function two(callback) […]

在callback中调用node.js中的助手函数?

我是相当新的编程与node.js,我不知道为什么我得到这个错误。 该函数看起来设置正确,我不认为我有任何asynchronous问题B / C这些应该考虑我自己的地方(我认为)的自variables。 我也尝试没有这个,使用简单的变种consolePrint(…)无论如何,这是我的代码在下面,错误日志下面。 /* global __dirname */ var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var self = this; //CALLING HELPER FUNCTION HERE var server = app.listen(8000, self.consolePrint(server)); app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use('/public', express.static(__dirname + '/public')); app.get('/', function (req, res) { res.sendFile(__dirname + '/public/views/index.html'); }); //—————helper function(s)——————-// self.consolePrint […]

在服务器上运行一个asynchronous的方法

我有一个meteor服务器方法,执行一个函数,我想在后台运行。 我希望服务器方法继续,而函数运行在不同的过程中。 方法: myMethod: function(arg) { _event.emit("myEvent",args); …do some other stuff } 我希望服务器做其他的事情,并返回到客户端,并在后台执行_event.emit中的东西,没有结果必须发回客户端。 它目前所做的是在_event.emit中运行这些东西,然后返回到客户端。 我尝试通过在服务器端调用一个空的callback函数来解决这个问题,但这并没有办法 myMethod: function (arg) { return Meteor.call("secondMethod", _.toArray(arg), function() {}) } secondMethod: function (arg) { _event.emit("myEvent",args); } 希望有人知道这个解决scheme。 谢谢

在Meteor中,Meteor.wrapAsync是否阻塞其他呼叫?

在Meteor JS代码中,我使用HTTP.get方法在一个方法内调用服务器。 我必须返回结果给客户端,所以我用Meteor.wrapAsync包装这个函数来获得一个同步函数。 var httpSync = Meteor.wrapAsync(HTTP.get, this); var result = httpSync(myUrl); 我的问题是 – 将Meteor.wrapAsync(AsyncFunction)阻止其他请求? 它会影响多个请求的并行执行吗?

在Javascript中编写asynchronous方法(在Node.js中运行代码)

在下面的代码中,我试图理解编写asynchronousJavaScript函数的概念。 要明确我的假设: 函数logTest()被调用。 它调用asyncTest(),传入log()函数。 但是,asyncTest()不直接调用log()。 而是将其传递给事件队列,以在堆栈清除时被调用。 在for循环完成之前,堆栈不应该清除,写入20个“waiting …”。 但是,控制台输出在for循环之前先放置“async”行。 我在Node.js中运行这个代码,其中console.log是一个同步函数。 有谁知道为什么“asynchronous”行不写最后? function asyncCall(method) { return setTimeout(method, 0); } function log(str) { console.log(str); } function logTest() { asyncCall(log("async")); for(var i = 0; i < 20; i++) { log("waiting…"); } } logTest();

在Nodejs上同步读取或写入?

为什么以及什么时候我们应该更喜欢read/writeFileSync到Nodejs上的asynchronous服务器,特别是服务器应用程序?