JavaScript/Node.js JSON.parse() Undefined

I am receiving the error:

undefined:1
'{"completed_in":0.078,"max_id":333655176038719488,"max_id_str":"3336551760387
^
SyntaxError: Unexpected token '
    at Object.parse (native)
    at /home/tweets/10seconds.js:25:25
    at passBackControl (/home/tweets/node_modules/oauth/lib/oauth.js:367:11)
    at IncomingMessage.<anonymous> (/home/tweets/node_modules/oauth/lib/oauth.js:386:9)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:895:16
    at process._tickCallback (node.js:415:13)

When parsing Twitter API JSON from the Search API.

The code I am using the parse this JSON is:

MongoClient.connect("mongodb://localhost:27017/db", function(err, db) {

 if(err) { return console.dir(err); }
  var collection = db.collection("tweets");

    while(run < 200){

        oa.get("http://search.twitter.com/search.json?q=morning&rpp=100&include_entities=true&result_type=mixed", access_token, access_token_secret, function(error, data) {
            var theTweets = JSON.parse(sys.inspect(data));

           collection.insert(theTweets, {w:1}, function(err, result) {});
           console.log(run);
        });
    run = run + 1;
    }

});

What could cause this?

Probably, the output of sys.inspect is not JSON it adds the quote '.

Why are you using sys.inspect?

If data is a JSON string, then use JSON.parse(data); and if it's an object already, well you don't have to use JSON.parse at all...

The problem is on the sys.inspect() call as already stated by the other answers and comments.

As stated in the nodejs documentation (http://nodejs.org/api/util.html#util_util_inspect_object_options):

Return a string representation of object, which is useful for debugging.

By the way, it seems they changed the sys module name to util.

You can easily make a small test by running the following code on an older node version. It should throw your error:

var sys = require('sys');
var data = '{"test":1, "test2":2}';
sys.puts(JSON.parse(sys.inspect(data)));

And then remove the sys.inspect call. It should correctly parse the object:

var sys = require('sys');
var data = '{"test":1, "test2":2}';
sys.puts(JSON.parse(data));