![](/img/trans.png)
[英]undefined is not an object on react-native expo with firestore
[英]How should I convert a firestore object into something I can use in expo/react-native?
这是我用来从我的 firestore 实例中获取所有列表的 function。 我希望能够检索列表并将它们添加到我的列表页面。
`function getListings() {
db.collection('listings').get().then((snapshot)=> {
snapshot.docs.forEach(doc => {
const id = doc.id
const data = doc.data()
console.log({id,data})
})})`
这是登录控制台的内容。 2 个单独的列表。 两个对象。
`Object {
"data": Object {
"amount": Object {
"amount": "1000 ",
},
"category": Object {
"category": Object {
"backgroundColor": "purple",
"label": "Fruits",
"value": 1,
},
},
"dateToday": Object {
"dateToday": "Todays Date",
},
"description": Object {
"description": "Crystal Clear",
},
"imageUris": Object {},
"title": Object {
"title": "Spring Water",
},
"userId": Object {
"userId": "User Name",
},
},
"id": "CB59J7NL7IXgKsUU6IyS",
}
Object {
"data": Object {
"amount": Object {
"amount": "99",
},
"category": Object {
"category": Object {
"backgroundColor": "purple",
"label": "Fruits",
"value": 1,
},
},
"dateToday": Object {
"dateToday": "Todays Date",
},
"description": Object {
"description": "Granny Smith",
},
"imageUris": Object {},
"title": Object {
"title": "Apples",
},
"userId": Object {
"userId": "User Name",
},
},
"id": "Tr2GjKES85HaSdeEOZwa",
}`
如何将 object 转换为我可以使用的数据? 提前致谢!
您首先应该创建一个 class。我调用了 MyModel。
class MyModel {
id="";
data = "";
category = "";
dateToday = "";
description ="";
imageUris ="";
userId ="";
constructor(props) {
this.data = props && props.data || this.data;
this.category = props && props.category || this.category;
this.dateToday = props && props.dateToday || this.dateToday;
this.description = props && props.description || this.description;
this.imageUris = props && props.imageUris || this.imageUris;
this.userId = props && props.userId || this.userId;
}
}
之后更新 function getListings()
如下所示。
function getListings() {
db.collection('listings').get().then((snapshot) => {
const myObjects = snapshot.docs.map((doc) => new MyModel({ ...doc.data(), id: doc.id }))
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.