firebaseのデータ一覧を表示したいのですがうまくできません。
画面起動時にエラーが出ず、
console.log(doc.data())
でtitleなどの値が取れていて、
console.log(AllHandsOn)
でPromise {<pending>}
の値が取れてます
doc.data().titleで表示しようとしたのですが、
できず
import React from 'react' import { makeStyles } from '@material-ui/core/styles' import Card from '@material-ui/core/Card' import CardActionArea from '@material-ui/core/CardActionArea' import CardActions from '@material-ui/core/CardActions' import CardContent from '@material-ui/core/CardContent' import CardMedia from '@material-ui/core/CardMedia' import Button from '@material-ui/core/Button' import Typography from '@material-ui/core/Typography' import Divider from '@material-ui/core/Divider' import { db } from '../../firebase/index' const useStyles = makeStyles({ root: { maxWidth: 345, }, }) export default function ImgMediaCard() { const classes = useStyles() const AllHandsOn = db .collection('handsOn') .get() .then(function (querySnapshot) { querySnapshot.forEach(function (doc) { console.log(doc.data()) console.log(AllHandsOn) // doc.data() is never undefined for query doc snapshots }) }) return ( <Card className={classes.root}> <CardActionArea> <CardContent> <Typography gutterBottom variant="h5" component="h2"> {Object.title} </Typography> <Typography variant="body2" color="textSecondary" component="p"> {AllHandsOn.url} </Typography> </CardContent> </CardActionArea> <Divider /> <CardActions> <Button size="small" color="primary"> {AllHandsOn.tag} </Button> <Button size="small" color="primary"> Learn More </Button> </CardActions> </Card> ) }
試した事
Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
import React from 'react' import { makeStyles } from '@material-ui/core/styles' import Card from '@material-ui/core/Card' import CardActionArea from '@material-ui/core/CardActionArea' import CardActions from '@material-ui/core/CardActions' import CardContent from '@material-ui/core/CardContent' import CardMedia from '@material-ui/core/CardMedia' import Button from '@material-ui/core/Button' import Typography from '@material-ui/core/Typography' import Divider from '@material-ui/core/Divider' import { db } from '../../firebase/index' const useStyles = makeStyles({ root: { maxWidth: 345, }, }) export default function ImgMediaCard() { const classes = useStyles() const handsOnRef = db .collection('handsOn') .get() .then(function (querySnapshot) { querySnapshot.forEach(function (doc) { // doc.data() is never undefined for query doc snapshots console.log(doc.id, ' => ', doc.data()) }) }) return ( <Card className={classes.root}> <CardActionArea> <CardContent> <Typography gutterBottom variant="h5" component="h2"> {handsOnRef} </Typography> <Typography variant="body2" color="textSecondary" component="p"> a </Typography> </CardContent> </CardActionArea> <Divider /> <CardActions> <Button size="small" color="primary"> a </Button> <Button size="small" color="primary"> Learn More </Button> </CardActions> </Card> ) }
②試したこと
TypeError: doc.data is not a function
import React from 'react' import { makeStyles } from '@material-ui/core/styles' import Card from '@material-ui/core/Card' import CardActionArea from '@material-ui/core/CardActionArea' import CardActions from '@material-ui/core/CardActions' import CardContent from '@material-ui/core/CardContent' import CardMedia from '@material-ui/core/CardMedia' import Button from '@material-ui/core/Button' import Typography from '@material-ui/core/Typography' import Divider from '@material-ui/core/Divider' import { db } from '../../firebase/index' const useStyles = makeStyles({ root: { maxWidth: 345, }, }) class ImgMediaCard extends React.Component { constructor(props) { super(props) this.state = { title: '', url: '', tag: '', } } componentDidMount() { const doc = db.collection('handsOn').get() const data = doc.data() if (data) { this.setState({ title: data.title, url: data.url, tag: data.tag, }) } } render() { return ( <Card> <CardActionArea> <CardContent> <Typography gutterBottom variant="h5" component="h2"> {this.state.title} </Typography> <Typography variant="body2" color="textSecondary" component="p"> {this.state.url} </Typography> </CardContent> </CardActionArea> <Divider /> <CardActions> <Button size="small" color="primary"> {this.state.tag} </Button> <Button size="small" color="primary"> Learn More </Button> </CardActions> </Card> ) } } export default ImgMediaCard
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/17 01:13
2020/10/17 01:23
2020/10/20 05:43