简体   繁体   English

Scope 带回调 function

[英]Scope with callback function

I have part of an if statement below that does a few api calls to facebook and instagram.我在下面有一个 if 语句的一部分,它对 facebook 和 instagram 做了一些 api 调用。 I am having trouble getting the 'instagram' part of this to push the data to an array... I am thinkig is a scope issue but not sure - Maybe you can tell me why I am getting nothing pushed into my array.我无法让“instagram”部分将数据推送到数组中......我认为这是一个 scope 问题,但不确定 - 也许你可以告诉我为什么我没有任何东西被推入我的数组。 The comments explain what happens.评论解释了会发生什么。 I cant get the instagram posts to push to the instaFormattedPosts array.我无法将 instagram 帖子推送到 instaFormattedPosts 数组。

else if(responseData.fbData && responseData.fbData.instaId){
            //First Get Fb Posts - I push them into this array, 'fbFormattedPosts' then use thatlater to update state. This one works, fbPost object gets pushed forEach() fbPosts.data response. 
            let fbFormattedPosts = []
            response = await fetch(`https://graph.facebook.com/${fbDataTemp.pageId}/feed?access_token=${fbDataTemp.pageAccessTokenLong}`)
            let fbPosts = await response.json()
            fbPosts.data.forEach(post => {
                if(post.message){
                    let fbPostObject = {
                        type: 'text',
                        data: post.message,
                        link: `http://www.facebook.com/${post.id}`,
                        date: post.created_time,
                        postId: post.id,
                        rockOn: []
                    }
                    fbFormattedPosts.push(fbPostObject)
                }
            })

            //Get IG Media Ids - This returns a list of instagram id's. 
            let instaFormattedPosts = []
            response = await fetch(`https://graph.facebook.com/v7.0/${fbDataTemp.instaId}/media?access_token=${fbDataTemp.pageAccessTokenLong}`)
            let instaPosts = await response.json()

            //Get IG Posts - Alright here is where I cant get the instaPostObject to push to isntaFormattedPosts array...
            instaPosts.data.forEach(async instaId => {
                const instaResponse = await fetch(`https://graph.facebook.com/${instaId.id}?fields=id,media_url,timestamp,username&access_token=${fbDataTemp.pageAccessTokenLong}`)
                let instaPostRendered = await instaResponse.json()

                let instaPostObject = {
                    type: 'instagram',
                    data: instaPostRendered.media_url,
                    link: `http://www.instagram.com/${instaPostRendered.username}`,
                    date: instaPostRendered.timestamp,
                    postId: instaPostRendered.id,
                    rockOn: [],
                }
                instaFormattedPosts.push(instaPostObject)
                console.log(instaPostObject) //Returns the object with the right details.
            })

            console.log(instaFormattedPosts) // returns empty array.. 


            //Set All Posts 
            setPosts([ 
                ...responseData.posts, 
                ...fbFormattedPosts
                   .filter(({postId}) => 
                      !responseData.posts
                         .find(post => post.postId == postId)),
               ...instaFormattedPosts
             ])
        }

async await don't work with forEach , it will just invoke async await不适用于forEach ,它只会调用

// this will just loop through all the data inside and instaPosts
// and not wait to complete inside block

instaPosts.data.forEach(async instaId => {
    // your code
    instaFormattedPosts.push(instaPostObject)
    console.log(instaPostObject) //Returns the object with the right details.
})

Solution: is simple for loop解决方案:很简单的for循环

for (let i=0 ; i< instaPosts.data.length ; i++) {
    const instaId = instaPosts.data[i];
    const instaResponse = await fetch(`https://graph.facebook.com/${instaId.id}?fields=id,media_url,timestamp,username&access_token=${fbDataTemp.pageAccessTokenLong}`)
    let instaPostRendered = await instaResponse.json()

    let instaPostObject = {
        type: 'instagram',
        data: instaPostRendered.media_url,
        link: `http://www.instagram.com/${instaPostRendered.username}`,
        date: instaPostRendered.timestamp,
        postId: instaPostRendered.id,
        rockOn: [],
    }
    instaFormattedPosts.push(instaPostObject)
    console.log(instaPostObject) //Returns the object with the right details.
}
console.log(instaFormattedPosts) // <---- CHECK NOW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM