如何从异步函数中获取某些数据?

How to get certain data from async function?

我正在尝试从异步函数获取数据,但是我无法获取它。

client.on('ready', () => {
IGclient
.login()
.then((data1) => {
  console.log('Data1: ', data1)
  IGclient
    .getProfile()
    .then(() => {
      (async () => {
        await IGclient.getUserByUsername({ username: "worldstar" })
      .then(res => console.log('Media: ', res.edge_owner_to_timeline_media.edges)) // it returns an array of your user's medias.
      console.log(res[0].shortcode);
      })()
    })
  })
})

来自 .then(res => console.log('Media: ', res.edge_owner_to_timeline_media.edges))

的回复
Media:  [
  {
    node: {
      __typename: 'GraphVideo',
      id: '2662044547635514500',
      shortcode: 'CTxe2l2FIyE',
      dimensions: [Object],
      display_url: 'https://instagram.fbts7-1.fna.fbcdn.net/v/t51.2885-15/e35/241737702_603358297699631_5052269439534742639_n.jpg?_nc_ht=instagram.fbts7-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=nCAjzZxBpX8AX8dIRn2&edm=ABfd0MgBAAAA&ccb=7-4&oh=68d55b1e54e94573e4a539c2dbd18688&oe=6141BC66&_nc_sid=7bff83',
      edge_media_to_tagged_user: [Object],
      fact_check_overall_rating: null,
      fact_check_information: null,
      gating_info: null,
      sharing_friction_info: [Object],
      media_overlay_info: null,
      media_preview: 'ABcqvg4CEE8gZ5J7f41TE5lbyicN1/z702GfMCux5XGT9Dz+lU1u08zfz8p46Zwev+fypW1C5svBxwx+nFFZ8l8cgLgiir0M9SpMJUUsoxHkZJHXP16ZqEzOUy2Cp4+YDJ7Z45AHbH61u3DCWGQdNv8A7KfX8KxokdpN8aK+R1fJUH15xk/mMnipL9CxDbJMPMZ9ufQcenfkZ+lFPe1l4aI7CB9eD/jRS+ZRoRRKDulPmMPX7o+i9P5mnAHPFQtVmHpSGEcIXOB1oq2lFID/2Q==',
      owner: [Object],
      is_video: true,
      has_upcoming_event: false,
      accessibility_caption: null,
      dash_info: [Object],
      has_audio: true,
      tracking_token: 'eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjp0cnVlLCJ1dWlkIjoiOGU3NTM2OGY2NGVlNGE1YjkyZTE5OWM2NjlhYzY0ZWEyNjYyMDQ0NTQ3NjM1NTE0NTAwIiwic2VydmVyX3Rva2VuIjoiMTYzMTU2MjU0ODYzOHwyNjYyMDQ0NTQ3NjM1NTE0NTAwfDM1NjczOTU5MDN8ZDA4YTU1NmIzYzYxYzljYWFiMDlmZDg5ZmFlZmEwM2QwYmEwNWJiNzkxYjJjNTBmOWU5NzdlOTQzNTM2YmQ0YSJ9LCJzaWduYXR1cmUiOiIifQ==',
      video_url: 'https://instagram.fbts7-1.fna.fbcdn.net/v/t50.2886-16/241879045_655893848721915_3756581789983524987_n.mp4?_nc_ht=instagram.fbts7-1.fna.fbcdn.net&_nc_cat=103&_nc_ohc=MKV6lvykxUIAX8y1V2t&edm=ABfd0MgBAAAA&ccb=7-4&oe=61421A71&oh=5fd9789d322617de1064d5e1618ba214&_nc_sid=7bff83',
      video_view_count: 92970,
      edge_media_to_caption: [Object],
      edge_media_to_comment: [Object],
      comments_disabled: false,
      taken_at_timestamp: 1631560467,
      edge_liked_by: [Object],
      edge_media_preview_like: [Object],
      location: null,
      thumbnail_src: 'https://instagram.fbts7-1.fna.fbcdn.net/v/t51.2885-15/e35/c0.210.540.540a/241737702_603358297699631_5052269439534742639_n.jpg?_nc_ht=instagram.fbts7-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=nCAjzZxBpX8AX8dIRn2&edm=ABfd0MgBAAAA&ccb=7-4&oh=222d3ead453326c4392261439f345998&oe=6141AF70&_nc_sid=7bff83',
      thumbnail_resources: [Array],
      felix_profile_grid_crop: null,
      coauthor_producers: [],
      product_type: 'clips',
      clips_music_attribution_info: [Object]
    }
  },

我需要从响应中选择 shortcode 以便我可以以某种方式比较它们,检测是否有新的 post 制作并将其分享到不和谐的频道..

console.log(res[0].shortcode); 将 return 这...

console.log(res[0].shortcode);
                  ^

ReferenceError: res is not defined

我如何才能只收到 shortcode,然后我才能以某种方式比较它们以检测新的 post? (不知道该怎么做嘿嘿..将是另一项研究...)

评论更新:

client.on('ready', () => {
IGclient
.login()
.then((data1) => {
  console.log('Data1: ', data1)
  IGclient
    .getProfile()
    .then(() => {
      (async () => {
        await IGclient.getUserByUsername({ username: "worldstar" })
        .then(res => { console.log('Media: ', res.edge_owner_to_timeline_media.edges);       
        console.log(res.edge_owner_to_timeline_media.edges.Media[0].node.shortcode); })
      })
    })
  })
})

这不会 return 不幸的是..甚至不是第一次登录。

您的代码有问题

您的代码有两个问题使其无法正常运行:

  1. 您在 res 变量退出其范围后调用它。
  2. 您试图错误地获取 res 对象的 shortcode 属性。

对于第一个问题,有几种方法可以解决这个问题,但最简单的方法是使用 以便在调用 [= 时将 res 变量保持在范围内16=]第二次。

对于问题 2,说 console.log(res[0].shortcode); 很可能会引发类型错误,因为 res 变量中不存在 属性 0,因此调用 res[0] 将 return undefined。尝试从 undefined 对象获取 属性 将导致 TypeError。根据您上面发布的对象的形状,这应该正确地从您的 res 对象中获取 shortcode 属性:

console.log(res.edge_owner_to_timeline_media.edges.Media[0].node.shortcode);

您可能可以通过执行以下操作来实现此功能:

client.on('ready', () => {
    IGclient
        .login()
        .then((data1) => {
            console.log('Data1: ', data1)
            IGclient
                .getProfile()
                .then(() => {
                    (async () => {
                        await IGclient.getUserByUsername({
                                username: "worldstar"
                            })
                            .then(res => {
                                console.log('Media: ', res.edge_owner_to_timeline_media.edges);
                                console.log(res.edge_owner_to_timeline_media.edges[0].node.shortcode);
                            })()
                    })
                })
        })
})

你对 .then 的使用

由给定 .then 的函数编辑的对象 return 包装在 Promise 中,这允许您将 .then 语句链接在一起。这允许更清晰、更少嵌套的代码。这可用于清理您的代码,如下所示:

client.on('ready', () => {
    IGclient
        .login()
        .then(data1 => {
            console.log('Data1: ', data1);
            return IGclient.getProfile();
        })
        .then(() =>
            IGclient.getUserByUsername({
                username: "worldstar"
            })
        ).then(res => {
            console.log('Media: ', res.edge_owner_to_timeline_media.edges);
            console.log(res.edge_owner_to_timeline_media.edges[0].node.shortcode);
        });
})