-
-
Save ccutch/6dd0b75276b6d31e6b08efb2493096e9 to your computer and use it in GitHub Desktop.
The only important line of HomeScreen.js is line 34. The variable assignment here returns a Promise but no data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as firebase from 'firebase' | |
import 'firebase/firestore'; | |
class FireBase { | |
constructor() { | |
let config = { | |
apiKey: "AIzaSyAO8BB0W6mXDr3ihvkMn-uJjopgfsf_qow", | |
authDomain: "cherishly-412dd.firebaseapp.com", | |
databaseURL: "https://cherishly-412dd.firebaseio.com", | |
projectId: "cherishly-412dd", | |
storageBucket: "cherishly-412dd.appspot.com", | |
messagingSenderId: "378187715149" | |
} | |
if (!firebase.apps.length) { | |
firebase.initializeApp(config) | |
} | |
const firestore = firebase.firestore(); | |
// Random thing required by firestore | |
const settings = {timestampsInSnapshots: true}; | |
firestore.settings(settings); | |
}; | |
// Retrieve data for specific collection and document | |
downloadData = (collectionId, documentId) => { | |
var ref = firebase.firestore().collection(collectionId).doc(documentId); | |
ref.get().then(function(doc) { | |
if (doc.exists) { | |
console.log("Document data:", doc.data()); | |
return doc.data(); | |
} else { | |
// doc.data() will be undefined in this case | |
console.log("No such document!"); | |
return null; | |
} | |
}).catch(function(error) { | |
console.log("Error getting document:", error); | |
return null; | |
}); | |
}; | |
// Retrieve all data within a collection | |
downloadAllData = async (collectionId) => { | |
var docList = []; | |
var ref = firebase.firestore().collection(collectionId); | |
// TODO: was missing a return so this function was returning null | |
return ref.get().then(function(querySnapshot){ | |
querySnapshot.forEach(function(doc){ | |
var docDict = {}; | |
docDict["id"] = doc.id; | |
docDict["data"] = doc.data(); | |
docList.push(docDict); | |
}); | |
// The doc list prints to console here | |
console.log(docList); | |
return docList; | |
}); | |
} | |
uploadData = (collectionId, documentId, data) => { | |
firebase.firestore().collection(collectionId).doc(documentId).set({ | |
data, | |
}) | |
}; | |
} | |
// Helper function to check if a list is empty | |
function isEmpty(obj) { | |
for(var key in obj) { | |
if(obj.hasOwnProperty(key)) | |
return false; | |
} | |
return true; | |
} | |
Fire = new FireBase(); | |
export default Fire; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import { ScrollView, StyleSheet, View, Button, Text, TouchableHighlight } from 'react-native'; | |
import Fire from '../components/firebase/Firebase'; | |
import Navbar from '../components/navbar'; | |
import AddButton from '../components/addbutton'; | |
import PopupWindow from '../components/popupwindow'; | |
import TestAddButton from '../components/testAddButton'; | |
export default class HomeScreen extends Component { | |
constructor(props) { | |
super(props); | |
this.handleAddPhotoButton = this.handleAddPhotoButton.bind(this); | |
this.handleTestButtonPress = this.handleTestButtonPress.bind(this); | |
this.data = []; | |
} | |
// Handle state of popup window here | |
state = { | |
popupWindow: false, | |
loadingData: false, | |
}; | |
// Event handler for add photo button | |
// Pass this handler to child component | |
handleAddPhotoButton = (e) => { | |
e.preventDefault(); | |
e.persist(); | |
this.setState(prevState => ({ | |
popupWindow: !prevState.popupWindow | |
})); | |
var dataPromise = Fire.downloadAllData('users'); | |
// TODO: this now returns a promise (all await functions do) so wait for | |
// `then` then you can use this.setState. | |
// alternatively you can use the async/await syntax over `.then` | |
dataPromise.then(data => { | |
// Here the doc data doesn't print to console | |
console.log(data); | |
// Fire.downloadData("sample", "test15") | |
// To upload to firebase | |
// Fire.uploadData("sample", "test15", {"egf":"blah"});; | |
console.log(this.state.popupWindow); | |
}); | |
} | |
handleTestButtonPress = (e) => { | |
e.preventDefault(); | |
console.log(this.data); | |
} | |
// Removes extra whitespace introducedß by navigation | |
static navigationOptions = { | |
header: null, | |
}; | |
render() { | |
const {navigate} = this.props.navigation; | |
return ( | |
<View style={{flex: 1}}> | |
<Navbar navigation={this.props.navigation} /> | |
<PopupWindow status={this.state.popupWindow} /> | |
<View style={styles.main}> | |
<ScrollView> | |
<Text>Cherishly is a photo sharing application!</Text> | |
</ScrollView> | |
</View> | |
<AddButton handler={this.handleAddPhotoButton} /> | |
<TestAddButton handler={this.handleTestButtonPress} /> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
main : { | |
flex: 9, | |
backgroundColor: 'white', | |
justifyContent: 'center', | |
alignItems: 'center' | |
}, | |
addButton: { | |
position: 'absolute', | |
bottom: 30, | |
right: 30, | |
width: 80, | |
height: 80, | |
zIndex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
borderRadius: 50, | |
backgroundColor: '#F35F64', | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment