微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

GET request Firestore with Ionic + Angular:跨域请求被阻止并返回空数组

如何解决GET request Firestore with Ionic + Angular:跨域请求被阻止并返回空数组

我正在学习如何在 Ionic + Angular 项目中使用 Firebase 函数

我想创建我的自定义函数,该函数获取所有游戏(一个集合)并按照名为“count”的属性的顺序返回包含这些游戏的数组。我的第一步是获取所有游戏并显示以了解结构。

我在用 CORS 函数封装的 index.ts 中创建了我的函数,并通过设置标题,如许多教程所示:

export const getMostPlayedGames = functions.https.onRequest((req,res) => {
    cors(req,res,() => {
        const arrayOfGames: Game[] = []

        admin.firestore().collection('games').get().then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                arrayOfGames.push(doc.data() as Game)
            })
        }).catch((err) => {
            console.log(err)
            res.send({text: err})
        })

        res.send({json: arrayOfGames}).set('Access-Control-Allow-Origin','*')
    })
})

这是我使用函数的 module.ts:

    private async callTopGames() {
        this.http
            .get('http://localhost:5001/gamebridge-app/us-central1/getMostPlayedGames',{responseType: 'json',headers: {'Access-Control-Allow-Origin': '*'}})
            .subscribe((data: any) => {
                // console.log(data)
                this.topGames = data.json
            })
    }

调用是通过 ionViewWillEnter 完成的:

  async ionViewWillEnter() {
      await this.callTopGames()
  }

当我尝试访问该页面时,我的控制台中有两个错误

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5001/gamebridge-app/us-central1/getMostPlayedGames. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5001/gamebridge-app/us-central1/getMostPlayedGames. (Reason: CORS request did not succeed).

在我的 IntelliJ 控制台中,Promise 仍然“待处理”:

>  Promise { <pending> }
>  []
>  {"structuredData":true,"severity":"INFO","message":"Get all games"}
i  functions: Finished "getMostPlayedGames" in ~1s

我尝试了很多可能性,但没有解决方案...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?