SloppierKitty7
SloppierKitty7

Reputation: 81

New to JSON in nodejs getting some errors

getJSON('https://api.twitch.tv/kraken/streams/Jonathan_x64', 
function(channel) {

    if (channel["stream"] == null) { 
        //do something

    } else {
        ////do something else 

    }
});

that is my current code but when i run it i get the following error

 if (channel["stream"] == null) {
               ^

TypeError: Cannot read property 'stream' of undefined
    at E:\my ultemet bot\index.js:10:16
    at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
    at Request.self.callback (E:\my ultemet bot\node_modules\request\request.js:200:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:1067:10)
    at emitOne (events.js:101:20)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:988:12)
    at emitNone (events.js:91:20)

Upvotes: 0

Views: 129

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146563

As far as I know there isn't a built-in top level getJSON() function in Node so you must be using a custom function.

From the stack trace you've shared:

at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
                                        ^^^^^^^^^^^^^^^^^^^^^

... we learn that you are using an NPM third-party module. Once there, it's trivial to find the documentation:

var getJSON = require('get-json')

getJSON('http://api.listenparadise.org', function(error, response){

    error
    // undefined

    response.result
    // ["Beth Orton &mdash; Stolen Car",
    // "Jack White &mdash; Temporary Ground",
    // "I Am Kloot &mdash; Loch",
    // "Portishead &mdash; Glory Box"]

    response.ok
    // => true

})

It isn't real code but it's clear that the first callback argument is error, but you have this:

function(channel){}

Since (as the error message states) it's undefined, that means that the call is successful—it's just you aren't reading it correctly.


I've been peeking the module source code and it's actually not very impressive. It's basically a tiny wrapper for request that doesn't add much value.

Upvotes: 2

Related Questions