【发布时间】:2019-06-01 22:13:57
【问题描述】:
我正在创建一个连接到 API 并允许用户将从 API 接收到的对象添加到其收藏夹的应用程序(React Native)。我已关注此tutorial,但收到黄色警告“可能未处理的 Promise Rejsction (id:0): TypeError: null is not an object (evalating 'firebase.auth().currentUser.uid')。数据正在添加到数据库,但它没有在我的应用程序中列出(显示)。
Home.js(我希望在其中列出我的最爱)
import * as firebase from 'firebase';
import {Container, Content, ListItem} from 'native-base';
var data = []
var currentUser
class Home extends React.Component {
constructor(props){
super(props)
this.ds = new ListView.DataSource({rowHasChanged:(r1,r2) => r1 !==r2})
this.state = {
listViewData : data
}
}
componentDidMount(){
this.getPlants()
}
getPlants = async()=>{
currentUser = await firebase.auth().currentUser
var that = this
firebase.database().ref(currentUser.uid).child('plantList').on('child_added',function(data){
var newData = [...that.state.listViewData]
newData.push(data)
that.setState({ listViewData: newData})
})
}
render(){
return(
<Container style={{ flex: 1, backgroundColor: 'yellow'}}>
<Content>
<ListView
enableEmptySections
dataSource = {this.ds.cloneWithRows(this.state.listViewData)}
renderRow={data =>
<ListItem>
<Text> {data.val().namePlant}</Text>
</ListItem>
}
/>
</Content>
</Container>
);}
}
export default Home;
App.js
import * as firebase from 'firebase';
firebase.auth().signInWithEmailAndPassword("mail@gmail.com","password")
...
export default class App extends Component {
constructor (props) {
super(props);
if (!firebase.apps.length) { firebase.initializeApp(config.FirebaseConfig); }
}
render() {
return <AppContainer />;
}
}
Plant.js(从 API 打印特定对象的打印数据并将其添加到数据库)
import * as firebase from 'firebase';
var currentUser
class CatalogPlant extends React.Component {
addToFavourites = async(scname) =>{
//get current user
currentUser = await firebase.auth().currentUser
//get unique key
var databaseRef = await firebase.database().ref(currentUser.uid).child('plantList').push()
//update plant name at the unique key
databaseRef.set({
'namePlant': scname
})
}
// part of code which connects to API and gets the data...
【问题讨论】:
-
请编辑您的问题以显示minimal, complete/standalone code that reproduces the problem。这包括删除重现问题不需要的任何内容,因为这与错误无关。见how to create a minimal, complete, verifiable example。
标签: javascript firebase react-native