.then 处理程序中未定义变量
Variable is not defined in a .then handler
我正在尝试使用 node-fetch
从 Google API 获取 JSON 输出。我设法成功记录了它,但是当我尝试将它分配给变量时出现错误。
我的代码:
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);
});
错误:
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.
虽然我确实从 .then((data) => console.log(JSON.stringify(data, null, 2)));
得到了 JSON 输出。
你的括号有误。在你的底部 .then
你用最后一个括号关闭函数,如果你将括号移出一层它应该可以工作。
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);
});
});
我正在尝试使用 node-fetch
从 Google API 获取 JSON 输出。我设法成功记录了它,但是当我尝试将它分配给变量时出现错误。
我的代码:
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);
});
错误:
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.
虽然我确实从 .then((data) => console.log(JSON.stringify(data, null, 2)));
得到了 JSON 输出。
你的括号有误。在你的底部 .then
你用最后一个括号关闭函数,如果你将括号移出一层它应该可以工作。
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);
});
});