Tag: foreach

MongoDB聚合,geNear并迭代callback

我有一个问题,我找不到解决scheme。 我有一些MongoSchemas用于存储来自用户的地理定位。 手机每隔5分钟发送一次经纬度。 这个API是完美的。 Mongo-Schema看起来像: // Importing Node packages required for schema const mongoose = require('mongoose'); const Schema = mongoose.Schema; //= =============================== // User Schema //= =============================== const GeolocationSchema = new Schema({ loc: { type: { type: String }, coordinates: { type: [Number], index: '2dsphere' } }, user: { type: Schema.Types.ObjectId, ref: 'User' } }, […]

循环中的node.jsasynchronous函数?

我有一些node.js的问题。 我想要做的是获取“./"+req.user.email中的目录数组,并通过循环find它们的大小并添加一个表格行来输出,你可以在代码中看到。 最后,我不想使用res.send()发送所有的表行。 但是,我得到的唯一的输出是: <tr></tr> 为数组中的每个文件。 看起来,forEach函数并不等待readSizeRecursive。 readSizeRecursive函数是asynchronous的,我相信这是什么导致的问题,但我不知道我怎么能解决这个问题。 任何帮助将不胜感激,我也包括readSizeRecursive函数。 谢谢! var output = ""; fs.readdir("./" + req.user.email, function (err, files) { files.forEach(function(file){ output += "<tr>"; readSizeRecursive("./"+req.user.email+"/"+file, function (err, total){ output += '<td>' + file + '</td><td>' + total + '</td>'; }); output += "</tr>" }); res.send(output) }); readSizeRecursive(): // Function to find the size of […]

Async.waterfall,foreach和parsingJSON node.js. 需要一些build议

我的任务是parsing最后100个dota2游戏。 我从jiiin( https://github.com/jiin/dota2api )使用dota 2 API和小型lib, 我所有的,这是一个async.waterfall调用。 喜欢这个: exports.get = function(req, res, next) { var playerID = +req.params.id; var playerCounter = []; var playerInfo = { kills: [], deaths: [], assists: [], last_hits: [], denies: [], hero_damage: [], hero_healing: [], gold_spent: [], kills_number: 0, deaths_number: 0, assists_number: 0 }; async.waterfall([ function getDota2Json(callback) { dota.getByAccountID(playerID, function […]

Async.foreach迭代强制停止,直到第一次迭代执行

async.forEach(vsr.vehicles, function(vsr_vehicle, callback){ pjCustom.vehicleJson(vsr_vehicle, function(vehicleInitialize){ Vehicle.find({ where: { vehicleID: (vsr_vehicle.vehicleID).toString().trim() } }).success(function(vehicleFound){ if(vehicleFound){ //Code Logic is working fine. }else{ vehicleBuild.save().success(function(vehicleNew){ // To create new vehicle of updated vsr var vehicleBuild = Vehicle.build(vehicleInitialize) pj.log("Update vehicle ……………………….") temp.push(vehicleNew.vehicleID) }) } }) }) callback() },function(){ res.send(204) }) //vehicleJSON exports.vehicleJson = function(vsr_vehicle, callback){ pjCustom.getVehicle(vsr_vehicle, function(status, vehicleId){ if (status == […]

TypeError:Object#<Promise>没有方法forEach'mongodb

我正尝试在Visual Studio中使用MongoDB和nodejs将文档从一个集合复制到另一个集合。 这是我想要做的: var newcollection = db.get('mynewdata'); var oldcollection = db.get('myusercollection'); var userName = req.body.username; oldcollection.find({ "username" : userName }).forEach(function (docs) { newcollection.insert(docs); }); 在Mongo网站上,它说,collection.find()应该返回一个应该能够使用forEach()方法的游标。 但是,它并没有返回游标对象,forEach似乎没有为返回的内容定义。 我从主题行得到的错误说: TypeError: Object #<Promise> has no method 'forEach' 任何想法,为什么它给了我这个错误?

对于每个元素,单击更改值

我已经使用nodejs处理栏为每个表创build一个: <tbody> {{#each result}} <tr> <td id="engagementTotal">{{data.engagementTotal}}</td> </tr> {{/each}} </tbody> 但我需要通过onclick函数来更改每个值的值,我试图使用 for (var i=0; i < positiveTotal.length; i++){ sEngagementTotal.push(positiveTotal[i].data.engagementTotal); } document.getElementById("engagementTotal").innerHTML = sEngagementTotal; 但是上面只改变了第一个表的值

BitGO – 从对象数组中获取MIN值

Bitgo提供所有的交易作为对象,并始终保持在一个大arrays。 在嵌套的.entries里面,我们看到第一个TX对象有两个负值-312084680和-4254539,我只需要这个最低值。 我的代码工作并logging所有交易,但只有从一个地址发送货币才是正确的。 如果存在两个负值,则logging3个事务,而不是2个。 var allTxs = [ { "id": "e5216ffaaa2a37bcc14380db07f06c85a65bcdc4e1fcab2bd5523f0b8a11bc15", "normalizedHash": "0709c99097386a3c0130f3d6b002acf6a4e37978406704268fc9d308eec4c2b8", "date": "2016-04-17T20:07:03.700Z", "fee": 7440, "inputs": [ { "previousHash": "6d043a06ade4eac5315967c463fcd65deb4ed9bff23ee3e73ff82c9cf72360e9", "previousOutputIndex": 1 }, { "previousHash": "b6e566cbee0f23bee7b321eda7f6159a165101e77e7f1e75bd9eb6e31540b391", "previousOutputIndex": 0 } ], "outputs": [ { "vout": 0, "account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q", "value": 312500000, "isMine": true, "chain": 0, "chainIndex": 0 }, { "vout": 1, "account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe", "value": 3831779 […]

循环节点无意中改变了对象

我有一个angular度的应用程序,谈到一个node.js套接字服务器,谈话node.js应用服务器。 在这个套接字服务器函数中,我想根据哪个用户来清理应用服务器的响应。 我把一个拷贝放到了被擦净的物体的手上,然后把这个拷贝发送给客户端。 它看起来像副本影响的原始对象,因为循环停止后,第一个和其他客户端没有得到消息。 第一个控制台输出显示第二个客户端已连接,并在数据操作之前显示在clientsSockets列表中。 所以我应该只看到“用户没有连接”4次。 第二个控制台输出显示原始对象hand.players在从未调用时正在更改。 这是我的问题。 为什么hand.players只有当emitHand.players在代码中改变时才会改变? 我也尝试了一个forEach(注释掉)无济于事。 socket.on('hand', function(receivedHand) { console.log('clientSockets[56fde5327ee729ac1a37fd1c].userId '+ util.inspect( clientSockets['56fde5327ee729ac1a37fd1c'].userId, false, null)); var hand = null; PostHand(receivedHand).then(function(data) { hand = JSON.parse(data); console.log('hand.players'+util.inspect(hand.players, false,null)); var emitHand=hand; // emitHand.players.forEach(function(player, index, handPlayers) { for(var index in emitHand.players){var player=emitHand.players[index]; if (clientSockets[player.userId]!=null){ ScrubHand(emitHand.players,index).then(function(data) { emitHand.players=data.emitHandPlayers; clientSockets[data.emitHandPlayers[data.index].userId].socket.emit('hand', emitHand); console.log('hand.players'+util.inspect(hand.players, false,null)); }); }else{ console.log('user not […]

如何在node.js中使用ejs从特定文件夹中使用每个图像?

我想从一个特定的文件夹中获取每个图像,并在一个页面上使用它。 图像具有随机名称,但现在只有jpg格式,将来可能会改变。 我可以用ejs来做吗,还是必须使用jQuery?

如何将值推送到节点js中的一个对象数组

"default_tabs" : [ { "value" : "Ice" }, { "value" : "Gold" } ] 我想将这个值分配到另一个数组中,使其看起来像下面的样子 selected_values : [{"values" : { "Ice" : "Edit","Gold" : "Edit" },"role" : "Admin"}] 为此我准备下面, default_tabs.forEach(function(i,v){ selected_values.push('values':v) }) 我知道我错了谁能帮助我please.Thanks。