2015-05-09 97 views
0

我想创建NSQ主题并在rethinkdb插入数据,但我无法将数据插入到rethinkdb中。谁能帮我。不插入数据到rethinkdb

**var nsq = require('nsqjs'); 
var r = require('rethinkdb'); 
var nsqdd = (process.env.NSQD_RETH || "localhost:4161").split(","); 
var connection = null; 
r.connect({host: 'localhost', port: 28015, db:'test', authKey:''}, function(err, conn) { 
    if (err) throw err; 
    connection = conn; 
}) 
var eventreader; 
eventreader = new nsq.Reader('ev_topic', 'ev_channel', { 
    lookupdHTTPAddresses: nsqdd 
}); 
eventreader.connect(); 
eventreader.on('message', function (msg) { 
    r.table('rethinkdb_test').insert(msg.json()).run(conn); 
    console.log('Received message [%s]: %s', msg.id, msg.body.toString()); 
    msg.finish(); 
});** 

回答

2

这似乎是你的第二个查询(在insert)不具有访问conn变量。为此,您需要将事件阅读器代码放在的连接函数回调中。

var nsq = require('nsqjs'); 
var r = require('rethinkdb'); 
var nsqdd = (process.env.NSQD_RETH || "localhost:4161").split(","); 
var connection = null; 
r.connect({host: 'localhost', port: 28015, db:'test', authKey:''}, function(err, conn) { 
    if (err) throw err; 
    connection = conn; 
    // Event Reader functionality inside connect callback 
    var eventreader; 
    eventreader = new nsq.Reader('ev_topic', 'ev_channel', { 
     lookupdHTTPAddresses: nsqdd 
    }); 
    eventreader.connect(); 
    eventreader.on('message', function (msg) { 
     // Now we have access to the connection 
     r.table('rethinkdb_test').insert(msg.json()).run(conn); 
     console.log('Received message [%s]: %s', msg.id, msg.body.toString()); 
     msg.finish(); 
    }); 
}); 
+0

非常感谢你 –