2 node.js firebase react-native google-cloud-firestore
我已经努力了几个小时,但对于这么简单的事情来说似乎很难。我需要通过 id 从 Firebase 获取,这是我正在使用但不起作用的代码:
fetch_selected_restaurant = () => {
var ref = firebase.firestore().collection('restaurants').where("res_id", "==", "5").get();
}
Run Code Online (Sandbox Code Playgroud)
请记住,读取 firestore/firebase 数据库是一个异步操作。
如果你想通过id阅读文档,你必须这样称呼它:
var ref = firebase.firestore().collection('restaurants').doc(yourDocId).get()
Run Code Online (Sandbox Code Playgroud)
如果你还记得,上面几行我提到几乎所有使用 firestore 的操作都是异步的,阅读文档也不例外。调用get()后返回Promise。我看到你将这个承诺存储在ref变量中,那很好。
现在为了得到结果,你必须向这个 promise 请求结果。在这一步你可以得到你想要的:
ref.then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
940 次 |
| 最近记录: |