2017-03-16 30 views
0

我有一個節點JS和rethinkdb模塊的問題。 我目前正在開發的程序我的論文Rethinkdb Node.JS .changes()不需要的循環

這件db.js代碼:

var r = require("rethinkdb"); 
var host = "localhost"; 
var db = "example"; 
var port = 28015; 
class dbs{ 
    connectToDb(callback){ 
     r.connect({ 
     host:host, 
     port:port, 
     db:db 
     },function(err,connection){ 
      return callback(err,connection); 
      }); 
    } 

    streamAllData(tableName,callback){ 
    this.connectToDb(function(err,conn){ 
    r.table(tableName).changes().run(conn,function(err,cursor){ 
     if(err){ 
     return callback(true,err); 
     } 
     else{ 
     cursor.next(function(err,rows){ 
      return callback(null,rows); 
     }); 

     } 
    }); 
    }); 
} 
} 

這件代碼server.js

var dbrs = require("./db"); 
var rdb = new dbrs(); 
var io = require("socket.io").listen(https.createServer(options,app).listen(port)); 
io.on("connection",function(socket){ 
    rdb.streamAllData("userlocation",function(err,data){ 
     socket.emit("broadcast:userlocation",data); 
    }); 
}); 

該結果總是發送7個相同的數據。實際上手機發送配套服務器到配置的時間間隔是乾淨的。

當我嘗試將驅動程序位置繪製到地圖時,這種不需要的循環總是在瀏覽器中崩潰。

enter image description here

是從鉻控制檯

回答

1

你的方法名streamAllData截圖不符合您的使用cursor.next,只取一個結果。也許你打算用cursor.each來代替?

請參閱https://www.rethinkdb.com/api/javascript/next/

+0

當然。因爲我需要每個更改的行都有單個結果,然後我處理每一行。 – Reza