不向 rethinkdb 插入数据

not insert data to rethinkdb

我正在尝试创建 NSQ 主题并在 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();
});**

您的第二个查询(insert)似乎无法访问 conn 变量。为此,您需要将事件 reader 代码 放入连接函数的回调 中。

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();
    });
});