So what I try to do is very simple: just receive data from Cloud Firestore.
I have following code:
import React from 'react';
import firebase from "react-native-firebase";
export default class newsFeed extends React.Component {
constructor() {
this.ref = firebase.firestore().collection('keys')
}
async load(id) {
const doc = await this.ref.doc(id).get()
if (doc.exists) {
return doc.data()
}
}
}
I receive an error: 'Cannot set property 'ref' of undefined'.
How can I fix it and what's the problem?
Learned it from this tutorial: https://medium.com/react-native-training/firebase-sdk-with-firestore-for-react-native-apps-in-2018-aa89a67d6934
通常箭头功能会有所帮助。
load = async (id) => { //...
You are exporting the class without creating an instance. You should do something like:
import React from 'react';
import firebase from "react-native-firebase";
class NewsFeed extends React.Component {
constructor() {
this.ref = firebase.firestore().collection('keys')
}
async load(id) {
const doc = await this.ref.doc(id).get()
if (doc.exists) {
return doc.data()
}
}
}
export default newsFeed = new NewsFeed();
Hope that helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.