Search code examples
javascriptnode.jspromisees6-promise

Variable is not defined in a .then handler


I am trying to get the JSON output from a Google API using node-fetch. I manage to log it successfully but when I try to assign it to a variable I get an error.

My code:

client.on("ready", () => {
  fetch(url)
    .then((res) => res.json())
    .then((data) => console.log(JSON.stringify(data, null, 2)));
  console.log("##########################################################################");
  search1 = JSON.stringify(data, null, 2);
  console.log(search1);
});

The error:

UnhandledPromiseRejectionWarning: ReferenceError: data is not defined
    at Client.<anonymous> (C:\Users\kalat\Documents\discord_code\src\main.js:15:28)
    at Client.emit (events.js:375:28)
    at WebSocketManager.triggerClientReady (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17)
    at WebSocketManager.checkShardsReady (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10)
    at WebSocketShard.<anonymous> (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14)
    at WebSocketShard.emit (events.js:375:28)
    at WebSocketShard.checkReady (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12)
    at WebSocketShard.onPacket (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16)
    at WebSocketShard.onMessage (C:\Users\kalat\Documents\discord_code\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\kalat\Documents\discord_code\node_modules\ws\lib\event-target.js:132:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14484) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14484) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I do get the JSON output from .then((data) => console.log(JSON.stringify(data, null, 2))); though.


Solution

  • You have a mistake with your brackets. In you bottom .then you are closing the function with the final bracket, if you move the bracket out one layer it should work.

    client.on("ready", () => {
        fetch(url)
        .then((res) => res.json())
        .then((data) => {
            console.log(JSON.stringify(data, null, 2));
            console.log("##########################################################################");
            search1=JSON.stringify(data, null, 2);
            console.log(search1);
        });
    });