小编Ant*_*MME的帖子

如何从客户端使用 Firestore 模拟器

我想在本地测试我的 firebase 功能。这些函数进行 firestore 查询。

所以我启动模拟器firebase emulators:start并在我的客户端中使用firebase.functions().useFunctionsEmulator('http://localhost:5001').

当我在客户端调用它们时,我的函数运行良好。我可以在 firestore 模拟器中读/写数据。

问题 :

我想直接在我的客户端中读取 firestore 模拟器数据,例如:

firebase.firestore().collection('tests').get().then(tests => {
    console.log( tests.docs.map(test=>test.map) )
})
Run Code Online (Sandbox Code Playgroud)

但我找不到如何在我的客户端中设置 firestore 模拟器。

这是我尝试过的:

1) Firestore 设置

firebase.firestore().settings({
    host:'http://localhost:8080',
    ssl:false
})
Run Code Online (Sandbox Code Playgroud)

结果 :

@firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.进入我的客户端控制台。

http 请求返回“未找到”

2)在我的 firebaseConfig 中设置模拟器 url

var firebaseConfig = {
    // ...
    databaseURL: "http://localhost:8080",
    // ...
}

firebase.initializeApp(firebaseConfig)

Run Code Online (Sandbox Code Playgroud)

在这种情况下,将请求远程服务器 ( https://firestore.googleapis.com ..)。

所以我想设置这两种情况之一:

1) …

javascript firebase firebase-tools google-cloud-functions google-cloud-firestore

12
推荐指数
2
解决办法
9090
查看次数