-1

this is my code ----

var nsq = require('nsqjs');
var r = require('rethinkdb');
var nsqvar = (process.env.NSQD_RETH || "localhost:4161").split(",");
    var p = r.connect({host:'localhost', port:8080, db:'test', authKey:''});{
        p.then(function(conn) {
            console.log("Succesfull connection")
        }).error(function(error) {
            console.log("Error at  connection")
        })
        // Event Reader functionality inside connect callback
        var eventreader;
        eventreader = new nsq.Reader('hello_topic', 'hello_channel', {
            lookupdHTTPAddresses: nsqvar
        });
        eventreader.connect();
        eventreader.on('message', function (msg) {
            // Now we have access to the connection
            r.table('sprinkle_nsq_test').insert(msg.json()).run(conn);
            console.log('Received message [%s]: %s', msg.id, msg.body.toString());
            msg.finish();
            console.log(msg);
        });
    }

And from the terminal I am trying to insert

curl -d '{"id": "712", "name": "Douglas Adams""type "casdasdasomedy"}' 'http://127.0.0.1:4151/put?topic= hello_topic'

At nsq at receives the message but at nodejs program at says throw new Error("Invalid JSON in Message"); ^ Error: Invalid JSON in Message

and also at same time the message is not storing at rethinkdb.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Istiak Mahmood
  • 2,330
  • 8
  • 31
  • 73
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders May 10 '15 at 16:49
  • What is the output of `msg.json()`? Can you console.log it and post it? – Jorge Silva May 10 '15 at 17:20
  • Also, your `insert` statement doesn't have access to your `conn` variable. They are in different scopes. Basically, your `conn` variable is `undefined` when you pass it to `run`. So your query won't execute. – Jorge Silva May 10 '15 at 17:22
  • Also, if you're running RethinkDB with its default settings, you have to connect to port 28015 not 8080. 8080 is the web admin port and the 28015 is the client driver port. – Jorge Silva May 10 '15 at 17:23

2 Answers2

2

Looks like the error message is correct - your JSON is invalid.

Try copying and pasting it through an online JSON validator / viewer and it will be invalid.

I've cleaned it up below. Hope it all works now.

{"id": "712", "name": "Douglas Adams", "type": "casdasdasomedy"}
Will
  • 6,561
  • 3
  • 30
  • 41
  • it still show the same error ---- throw new Error("Invalid JSON in Message"); ^ Error: Invalid JSON in Message – Istiak Mahmood May 10 '15 at 16:34
  • @Christofer Please post your new `curl` line the please, so I can see what you're sending – Will May 10 '15 at 16:41
  • curl -d '{"id": "712", "name": "Douglas Adams", "type "comedy"}' 'http://127.0.0.1:4151/put?topic=hello_topic' – Istiak Mahmood May 10 '15 at 16:51
  • 2
    That's still invalid! You haven't used the example I gave you, nor have you tested your JSON in either of the links I provided. This `"type "comedy"` , needs to be `"type": "comedy"` – Will May 10 '15 at 17:02
  • i think i am actually doing something wrong at rethinkdb, cause when i try to see the connection at rethinkdb it show "error at connection", can you kindly look where i am mistaking at my node.js application – Istiak Mahmood May 10 '15 at 17:15
  • Sorry - not a JS developer, so I can't comment on that. All I can see is invalid JSON, and an error message saying "invalid JSON". I'd make sure the source JSON was fixed before you move onto something else. – Will May 10 '15 at 17:20
1

You have some mistakes in your code (in regards to RethinkDB at least). Here is a fixed solution with comments (this may or may not fix your problem):

var nsq = require('nsqjs');
var r = require('rethinkdb');
var nsqvar = (process.env.NSQD_RETH || "localhost:4161").split(",");
// Connect to the client driver port 28015
var p = r.connect({ host:'localhost', port:28015, db:'test' });

p.then(function(conn) {
    // Wait until the database is connected
    console.log("Succesfull connection");

    // Event Reader functionality inside connect callback
    var eventreader;
    eventreader = new nsq.Reader('hello_topic', 'hello_channel', {
        lookupdHTTPAddresses: nsqvar
    });
    eventreader.connect();
    eventreader.on('message', function (msg) {
        // Make sure msg.json() is actually valid json
        if (typeof msg === 'object' && msg !== null) {
          throw new TypeError('`msg` is already and object. It doesnt need to be convert to json');
        }
        var json = msg.json();
        if (typeof json !== 'object' && msg !== null) {
          throw new TypeError('`msg.json()` is not an object and cant be inserted into the database.');
        }
        // Now we have access to the connection
        r.table('sprinkle_nsq_test').insert(msg.json()).run(conn)
          .then(function () {
            // Wait until RethinkDB is done inserted the message to call `.finish`
            console.log('Received message [%s]: %s', msg.id, msg.body.toString());
            msg.finish();
            console.log(msg);
          });
    });
}).error(function(error) {
    console.log("Error at connection to the database");
});
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
  • Thank you so much Jorge, but somehow it shows --- console.log("Error at connection to the database"); i can't find out the problem – Istiak Mahmood May 10 '15 at 17:43
  • I guess you should make sure the RethinkDB instances is running and is listening in the specificed ports. What happens when you go to localhost:8080? Do you get the admin UI? What happens when you run `rethinkdb` in your terminal? – Jorge Silva May 10 '15 at 18:40
  • when i goto localhost:8080 i have the access of rethinkdb and when i write rethinkdb at terminal it shows ------ Listening for intracluster connections on port 29015 Listening for client driver connections on port 28015 error: Could not bind to http port: The address at localhost:8080 is reserved or already in use. – Istiak Mahmood May 10 '15 at 21:22
  • It seems that RethinkDB is running somewhere and the http port is bound to 8080 but the intracluster port and the client driver port are not set to their defaults. A quick fix to this might be running `rethinkdb -o 1` to offset all ports by one and then try connecting through port 28016. http://rethinkdb.com/docs/start-a-server/ – Jorge Silva May 11 '15 at 16:17
  • Christofer: What is the content of `error` when the connection fails? Maybe we will find some useful hints in the error message. – Daniel Mewes May 12 '15 at 18:01
  • Also please double-check that you're connecting to port 28015, not 8080. Port 8080 is the web console, not the port for connecting RethinkDB clients. – Daniel Mewes May 12 '15 at 18:04