React + Firebase database componentDidMount and snapshot not working
up vote
0
down vote
favorite
I am trying to get .on('value') to fire so I can retrieve data but something is not working correctly. 'Mounted' logs fine, but 'test' does not. Can somebody please explain what I am probably misunderstanding? Additionally, I would like to update the state as shown in the commented out code.
Thank you for your help!
import React, { Component } from 'react'
import './Creature.css'
import Emblem from '../../../img/emblem_macabre.png'
import Macabre from '../../../img/macabre.jpg'
import * as firebase from 'firebase';
class Creature extends Component {
constructor() {
super()
this.state = {
name: '',
cultId: ''
}
}
componentDidMount() {
console.log('mounted')
const creatureCards = firebase.database().ref().child('creatureCards')
console.log(creatureCards)
creatureCards.on('value', snap => {
console.log('test')
})
// var name = snap.child('name').getValue()
// var cultId = snap.child('cultId').getValue()
// this.setState({
// name: name,
// cultId: cultId
// })
}
render() {
return(
<div className="creatureCard">
<span className="creatureName">{this.state.name}</span>
<span className="creatureArt"><img src={Macabre} alt=""/></span>
<ul className="creatureAttributes">
<li className="creatureAttribute">R</li>
<li className="creatureAttribute">D</li>
<li className="creatureAttribute">I</li>
<li className="creatureAttribute">H</li>
</ul>
<span className="creatureDescription">
<p>Card descriptions and abilities. This section defines the card and its roll in the game.</p>
</span>
<div className="emblem">
<img src={Emblem} alt=""/>
</div>
</div>
)
}
}
export default Creature
javascript reactjs firebase firebase-realtime-database
add a comment |
up vote
0
down vote
favorite
I am trying to get .on('value') to fire so I can retrieve data but something is not working correctly. 'Mounted' logs fine, but 'test' does not. Can somebody please explain what I am probably misunderstanding? Additionally, I would like to update the state as shown in the commented out code.
Thank you for your help!
import React, { Component } from 'react'
import './Creature.css'
import Emblem from '../../../img/emblem_macabre.png'
import Macabre from '../../../img/macabre.jpg'
import * as firebase from 'firebase';
class Creature extends Component {
constructor() {
super()
this.state = {
name: '',
cultId: ''
}
}
componentDidMount() {
console.log('mounted')
const creatureCards = firebase.database().ref().child('creatureCards')
console.log(creatureCards)
creatureCards.on('value', snap => {
console.log('test')
})
// var name = snap.child('name').getValue()
// var cultId = snap.child('cultId').getValue()
// this.setState({
// name: name,
// cultId: cultId
// })
}
render() {
return(
<div className="creatureCard">
<span className="creatureName">{this.state.name}</span>
<span className="creatureArt"><img src={Macabre} alt=""/></span>
<ul className="creatureAttributes">
<li className="creatureAttribute">R</li>
<li className="creatureAttribute">D</li>
<li className="creatureAttribute">I</li>
<li className="creatureAttribute">H</li>
</ul>
<span className="creatureDescription">
<p>Card descriptions and abilities. This section defines the card and its roll in the game.</p>
</span>
<div className="emblem">
<img src={Emblem} alt=""/>
</div>
</div>
)
}
}
export default Creature
javascript reactjs firebase firebase-realtime-database
Does your user have permission to readcreatureCards
? You can easily detect this by adding an error callback:creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to get .on('value') to fire so I can retrieve data but something is not working correctly. 'Mounted' logs fine, but 'test' does not. Can somebody please explain what I am probably misunderstanding? Additionally, I would like to update the state as shown in the commented out code.
Thank you for your help!
import React, { Component } from 'react'
import './Creature.css'
import Emblem from '../../../img/emblem_macabre.png'
import Macabre from '../../../img/macabre.jpg'
import * as firebase from 'firebase';
class Creature extends Component {
constructor() {
super()
this.state = {
name: '',
cultId: ''
}
}
componentDidMount() {
console.log('mounted')
const creatureCards = firebase.database().ref().child('creatureCards')
console.log(creatureCards)
creatureCards.on('value', snap => {
console.log('test')
})
// var name = snap.child('name').getValue()
// var cultId = snap.child('cultId').getValue()
// this.setState({
// name: name,
// cultId: cultId
// })
}
render() {
return(
<div className="creatureCard">
<span className="creatureName">{this.state.name}</span>
<span className="creatureArt"><img src={Macabre} alt=""/></span>
<ul className="creatureAttributes">
<li className="creatureAttribute">R</li>
<li className="creatureAttribute">D</li>
<li className="creatureAttribute">I</li>
<li className="creatureAttribute">H</li>
</ul>
<span className="creatureDescription">
<p>Card descriptions and abilities. This section defines the card and its roll in the game.</p>
</span>
<div className="emblem">
<img src={Emblem} alt=""/>
</div>
</div>
)
}
}
export default Creature
javascript reactjs firebase firebase-realtime-database
I am trying to get .on('value') to fire so I can retrieve data but something is not working correctly. 'Mounted' logs fine, but 'test' does not. Can somebody please explain what I am probably misunderstanding? Additionally, I would like to update the state as shown in the commented out code.
Thank you for your help!
import React, { Component } from 'react'
import './Creature.css'
import Emblem from '../../../img/emblem_macabre.png'
import Macabre from '../../../img/macabre.jpg'
import * as firebase from 'firebase';
class Creature extends Component {
constructor() {
super()
this.state = {
name: '',
cultId: ''
}
}
componentDidMount() {
console.log('mounted')
const creatureCards = firebase.database().ref().child('creatureCards')
console.log(creatureCards)
creatureCards.on('value', snap => {
console.log('test')
})
// var name = snap.child('name').getValue()
// var cultId = snap.child('cultId').getValue()
// this.setState({
// name: name,
// cultId: cultId
// })
}
render() {
return(
<div className="creatureCard">
<span className="creatureName">{this.state.name}</span>
<span className="creatureArt"><img src={Macabre} alt=""/></span>
<ul className="creatureAttributes">
<li className="creatureAttribute">R</li>
<li className="creatureAttribute">D</li>
<li className="creatureAttribute">I</li>
<li className="creatureAttribute">H</li>
</ul>
<span className="creatureDescription">
<p>Card descriptions and abilities. This section defines the card and its roll in the game.</p>
</span>
<div className="emblem">
<img src={Emblem} alt=""/>
</div>
</div>
)
}
}
export default Creature
javascript reactjs firebase firebase-realtime-database
javascript reactjs firebase firebase-realtime-database
edited Nov 9 at 23:47
asked Nov 3 at 3:26
gumball
113
113
Does your user have permission to readcreatureCards
? You can easily detect this by adding an error callback:creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57
add a comment |
Does your user have permission to readcreatureCards
? You can easily detect this by adding an error callback:creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57
Does your user have permission to read
creatureCards
? You can easily detect this by adding an error callback: creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Does your user have permission to read
creatureCards
? You can easily detect this by adding an error callback: creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
The problem was that at some point I switched to firestore but was still making calls to the database.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The problem was that at some point I switched to firestore but was still making calls to the database.
add a comment |
up vote
0
down vote
The problem was that at some point I switched to firestore but was still making calls to the database.
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem was that at some point I switched to firestore but was still making calls to the database.
The problem was that at some point I switched to firestore but was still making calls to the database.
answered Nov 10 at 23:32
gumball
113
113
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53128137%2freact-firebase-database-componentdidmount-and-snapshot-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does your user have permission to read
creatureCards
? You can easily detect this by adding an error callback:creatureCards.on('value', snap => { console.log('test') }, error => { console.error(error) } )
– Frank van Puffelen
Nov 3 at 3:28
Yes. I copied and pasted that in and it did not show an error. Any other thoughts?
– gumball
Nov 3 at 3:52
Update: It actually did throw an error this time. My database and storage are both set to public though. I will reasearch this. Can you provide a possible solution, please?
– gumball
Nov 10 at 20:57