for循环不返回任何内容

时间:2020-05-14 14:53:23

标签: javascript reactjs for-loop

我最近在我的应用中添加了一些Facebook API调用。当用户访问特定的个人资料页面时,它将调用facebook api并更新该页面的帖子。

但是,当用户使用搜索引擎时,它只会从我的数据库中提取旧帖子-因为facebook API调用仅在用户个人资料内部进行。这意味着搜索引擎的用户页面已过时。我希望搜索引擎保持最新状态。

因此,我正在搜索页面上编辑此功能,以检查用户是否已链接Facebook,以及是否已链接,以更新搜索页面上的帖子。

这是我的代码,以及它如何与注释一起使用-新的部分,for()循环,不返回新的数据(带)。

const getBands = async (location) => {
    const token = await getTokenSilently();

    //Get Local Bands
    try {
        let response = await fetch(`/api/autoquotegenerators/homeBands/${location[0]}/${location[1]}`, {
            headers: {
                Authorization: `Bearer ${token}`,
            }
        })
        const newBands = await response.json()


        //Updating Facebook Posts if the band has integrated FB - This is the part that doesnt work.
        for(let i=0; i < newBands.length; i++){
            if(newBands[i].fbData){
                //Hold onto Old Posts - This includes the native posts from my website.
                let oldPosts = newBands[i].posts

                //Get Fb Posts
                let fbFormattedPosts = []
                let response = await fetch(`https://graph.facebook.com/${newBands[i].fbData.pageId}/feed?access_token=${newBands[i].fbData.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)
                    }
                })

                //Combine All New Posts with Old Posts - And Check to make sure there are no duplicates.
                let newPosts = [ 
                    ...oldPosts, 
                    ...fbFormattedPosts
                        .filter(({postId}) => 
                        !oldPosts
                            .find(post => post.postId == postId)),
                ]

                //Save the new posts to the Band Object
                newBands[i].posts = newPosts

                return newBands[i]
            }else {
                return newBands[i]
            }

        }

        //Set The Band Objects to React State
        if(newBands !== []){
            setBands(newBands)
        }

        //Get Tour Bands
        response = await fetch(`/api/autoquotegenerators/tourBands/${location[0]}/${location[1]}`, {
            headers: {
                Authorization: `Bearer ${token}`,
            }
        })
        const newTourBands = await response.json();
        if(newTourBands !== []){
            setTourBands(newTourBands)
        }

        setAllBands([ ...newBands, ...newTourBands])

    } catch (error) {
        console.log(error)
    }
}

1 个答案:

答案 0 :(得分:0)

我弄清楚了..花了我一点时间-我不需要返回band对象。我只需要更新帖子。我删除了return语句,它就像一个魅力。谢谢!