0

So I'm exporting a callback-function in a module like this:

(function() {

    let request = require("request");

    module.exports = function GithubApi(url, callback) {

        let options = {
            uri: url,
            headers: {
                "User-Agent": "Me",
                "Content-Type": "application/x-www-form-urlencoded"
            }
        };

        request(options, function(err, body) {

            let context = {
                issues: JSON.parse(body.body).map(function(issue) {
                    return {
                        title: issue.title,
                        comments: issue.comments,
                    };
                })
            };

            callback(context) // The callback
        });
    };
}());

And this callback works perfectly fine when I'm using it in my GET-request with express.js:

app.get("/", (req, res) => {
    let url = "some url";

    GithubApi(url, (data) => {

        res.render("../some-views", data);
    });
});

But when I add a socket-emit, the callback-function returns SyntaxError: Unexpected end of JSON input

app.get("/", (req, res) => {
    let url = "some url";

    GithubApi(url, (data) => {

        io.socket.emit("update", {message: data}); // adding this
        res.render("../some-views", data);
    });
});

Can't understand why the socket would interfere with the request and return an error with JSON. Can anybody help?

Jesper
  • 2,044
  • 3
  • 21
  • 50

2 Answers2

1

The probablem must be caused by the fact that body.body doesn't contain a valid JSON string.

When you run code like this:

JSON.parse(body.body)

you should always use try/catch because JSON.parse throws exceptions on bad JSON.

See those answers for more details:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • Yes, I can see that, but why would the "body.body" not contain a valid JSON just because I run a socket before it? Don't understand why and how that would interfere. – Jesper Mar 10 '17 at 14:27
  • It is a valid JSON when I don't have the socket-emit in the callback-function at app.get("/")..., but when I add the socket-emit, the body.body becomes non-valid JSON. – Jesper Mar 10 '17 at 14:34
  • @Jesper "why would the "body.body" not contain a valid JSON" - I don't know why, I'm just saying that sometimes JSON is invalid - which is obviously the case here - and that's why you need try/catch. Did you even test what does body.body actually contain? If you see what's there you may have some hint **why** it's not a valid JSON. – rsp Mar 10 '17 at 14:35
  • Yes, I did. It's a request to a github-api, and the body.body returns a perfectly valid JSON. It's when I implement the socket.emit in the callback-function that the body-body returns undefined, and can't understand why the socket would interfere with the request. Did implement the try...catch tough, thanks for the tip. – Jesper Mar 10 '17 at 14:42
0

So the problem was with the io.sockets.emit("update", {message: data});. For some reason, that interfered with the request(still don't know why tough). I guess it has something to do with the socket broadcasting to all channels, and that causes some kind of error, read something about it here.

So I changed the call to the callback-function to this:

GithubApi(orgs, repo, token, (data) => {

    io.of("/").emit("update", {message: data}); // This line made it work
    res.render("../views/home", data);
});
Community
  • 1
  • 1
Jesper
  • 2,044
  • 3
  • 21
  • 50