如何提取变量的内容并将它们放入常量? Node.js

How to I extract the contents of a variable and place them into a constant? Node.js

我正在尝试提取变量 topPost 的内容并将其放入 url 下的 const options。我似乎无法让它工作。我正在使用 snoowrap/Reddit API 和 image-downloader.

var subReddit = r.getSubreddit('dankmemes');
var topPost = subReddit.getTop({time: 'hour' , limit: 1,}).map(post => post.url).then(console.log);
var postTitle = subReddit.getTop({time: 'hour' , limit: 1 }).map(post => post.title).then(console.log);

const options = {
  url: topPost,
  dest: './dank_memes/photo.jpg'
}

async function downloadIMG() {
  try {
    const { filename, image } = await download.image(options)
    console.log(filename) // => /path/to/dest/image.jpg
  } catch (e) {
    console.error(e)
  }
}

图片下载器的推荐格式如下:

const options = {
  url: 'http://someurl.com/image.jpg',
  dest: '/path/to/dest'
}

async function downloadIMG() {
  try {
    const { filename, image } = await download.image(options)
    console.log(filename) // => /path/to/dest/image.jpg
  } catch (e) {
    console.error(e)
  }
}

downloadIMG()

所以看起来我必须在 ' ' 之间设置 url 格式,但我不知道如何从 var topPost 获取 url 并将其放在这些引号之间。

如有任何想法,我们将不胜感激。 谢谢!

topPost是一个Promise,不是最终值。

Promises existence 是为了轻松处理异步数据。异步数据是 returns 在 未来某个时间点 的数据,而不是即时的,这就是为什么他们有 then 方法。当 Promise 解析为一个值时,将调用 then 回调。

在这种情况下,图书馆将连接到 Reddit 并从中下载数据,这不是立即可以完成的事情,因此代码将继续运行和稍后 将在数据下载完成后调用 then 回调。所以:

var subReddit = r.getSubreddit('dankmemes');
// First we get the top posts, and register a "then" callback to receive all these posts
subReddit.getTop({time: 'hour' , limit: 1,}).map(post => post.url).then((topPost) => {
    // When we got the top posts, we connect again to Reddit to get the top posts title.
    subReddit.getTop({time: 'hour' , limit: 1 }).map(post => post.title).then((postTitle) => {
        // Here you have both topPost and postTitle (which will be both arrays. You must access the first element)
        console.log("This console.log will be called last");
    });
});
// The script will continue running at this point, but the script is still connecting to Reddit and downloading the data
console.log("This console.log will be called first");

使用此代码时遇到问题。您首先连接到 Reddit 以获得顶部 post URL,然后您再次连接到 Reddit 以获得 post 标题。就像在中间按 F5。简单地认为,如果在这些查询之间添加一个新的 post,您将得到错误的标题(并且您正在消耗双倍的带宽消耗,这也不是最佳选择)。正确的做法是在同一个查询中同时获取标题和 url。怎么做?,像这样:

var subReddit = r.getSubreddit('dankmemes');
// We get the top posts, and map BOTH the url and title
subReddit.getTop({time: 'hour' , limit: 1,}).map(post => {
    return {
        url: post.url,
        title: post.title
    };
}).then((topPostUrlAndTitle) => {
    // Here you have topPostUrlAndTitle[0].url and topPostUrlAndTitle[0].title
    // Note how topPostUrlAndTitle is an array, as you are actually asking for "all top posts" although you are limiting to only one.
});

但这也很奇怪。为什么不直接获取 post 数据呢?像这样:

var subReddit = r.getSubreddit('dankmemes');
// We get the top posts
subReddit.getTop({time: 'hour' , limit: 1,}).then((posts) => {
    // Here you have posts[0].url and posts[0].title
});

有一种方法可以用 async/await 摆脱 JavaScript 回调地狱,但我不打算进入这个问题,因为对于新手来说有点困难解释为什么不是同步代码,虽然看起来是这样。