如何将 snekfetch 结果放入对象中?

How to get snekfetch results into an object?(如何将 snekfetch 结果放入对象中?)
本文介绍了如何将 snekfetch 结果放入对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个 discord.js 机器人并尝试使用 Node.js/snekfetch 调用天气 API.问题是我不知道如何将 API 返回的数据放入 javascript 对象中.我想做这样的事情:

I'm writing a discord.js bot and trying to call a weather API using Node.js / snekfetch. The issue is I can't figure out how to just put the data returned from the API into a javascript object. I want to do something like this:

let [country, ...city] = args;
let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;

var weatherObject;
snekfetch.get(url).then(r => weatherObject = r.body);`

但显然这不起作用所以我做错了什么?看起来它应该非常简单,但我就是做不到.由于 snekfetch 似乎没有被广泛使用,我在谷歌上搜索的任何内容都没有帮助,而且我完全无法将我所了解的有关 Promise 的任何信息外推到这种情况.

but obviously this doesn't work so what am I doing wrong? It seems like it should be insanely simple but I just can't do it. Nothing I've googled has helped since snekfetch doesn't seem to be widely used and I've been completely unable to extrapolate anything I've learned about promises to this scenario.

澄清:

snekfetch.get(url).then(r => console.log(r.body));

完全按照预期将对象打印到控制台,而

prints the object exactly as expected to the console, while

snekfetch.get(url).then(r => weatherObject = r.body);
console.log(weatherObject);

打印未定义..then() 语句的工作方式有什么我遗漏的吗?

prints undefined. Is there something I'm missing with how .then() statements work?

推荐答案

.then() 语句不会让程序等待它们完成,它们只是在 <一个 href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="nofollow noreferrer">Promise 他们被解决了.
这意味着您不能可靠地使用已在 Promise 中设置的值,因为 Promise 之后的代码可能会在该 Promise 解析之前执行.

.then() statements don't make the program wait for them to be completed, they just execute their code after the Promise they're attached to is resolved.
That means that you can't reliably use the value that has been set inside a Promise, because the code after the Promise will probably be executed before that Promise resolves.

您可以决定将其余代码移到该 .then() 语句中,也可以使用 async/await.
如果您在函数内部,则可以将其声明为 async function:允许你使用 await 关键字在里面.await使程序等待 Promise 解决,而不是 Promise,它返回您将在 .then() 函数中使用的值.
这是一个例子:

You can either decide to move the rest of the code inside that .then() statement or use async/await.
If you are inside a function, you can declare that as an async function: that allows you to use the await keyword inside it. await makes the program wait for a Promise to resolve, and instead of a Promise it returns the value that you would use in the .then() function.
Here's an example:

// Instead of using this: 
function getResult(args) {
  let [country, ...city] = args;
  let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;

  var weatherObject;
  snekfecth.get(url).then(response => {
    weatherObject = response.body;
  });

  return weatherObject; // undefined :(
}

// You could write it like this:
async function getResult(args) {
  let [country, ...city] = args;
  let url = `http://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&units=metric&APPID=${config.weatherID};`;

  let response = await snekfecth.get(url);
  var weatherObject = response.body;

  return weatherObject; // right value
}

这篇关于如何将 snekfetch 结果放入对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Auto-reply to direct messages(自动回复直接消息)
Play local music files using djs v13(使用 djs v13 播放本地音乐文件)
Making a bot delete its own message after a timeout(让机器人在超时后删除自己的消息)
How do I upload a custom emoji to my discord bot(如何将自定义表情符号上传到我的不和谐机器人)
Discord.js meme command with meme subreddit returns image as 403 forbidden(带有 meme subreddit 的 Discord.js meme 命令将图像返回为 403 禁止)
Get user banner in discord.js(在 discord.js 中获取用户横幅)