我在进一步处理从getPostedPlaces()
返回的Promise时遇到问题。当我运行getAll()
时,它会打印出下面所示的数组。这个数组看起来是正确的,但我如何让getAll()
函数返回一个实际的数组。我只能打印它,但不能返回数组本身。我还尝试过将数组中的对象推到数组上,但它返回为空。令人沮丧。
const getPostedPlaces = async () => {
const querySnapshot = await getDocs(collection(db, "posted_places"));
const newSnapshot = querySnapshot.docs.map(doc => {
let newDoc = doc.data();
newDoc.id = doc.id;
return newDoc;
});
return newSnapshot;
}
const getAll = () => {
getPostedPlaces().then(res => console.log(res))
}
阵列:
Array [
Object {
"active": true,
"category": Array [
"tøj",
"møbler",
],
"email": "bar@gmail.com",
"house_nr": 1,
"id": "i3juWf6Rj4OPBoKAjkBD",
"location": Object {
"latitude": 55.69718057,
"longitude": 12.52470763,
},
"price_interval": Array [
100,
700,
],
"street_name": "Hvidkildevej",
"time_end_actual": "",
"time_end_expected": "Sat Feb 05 2022 18:00:00 GMT+0100 (CET)",
"time_start": "Sat Feb 05 2022 09:00:00 GMT+0100 (CET)",
"zip_code": 2400,
},
]
由于使用{}
,因此需要添加退货
const getAll = async () => {
// since you use {} you need to use return below
return getPostedPlaces().then(res => {
console.log(res)
return res; //you need to return res here.
})
}
然后当你使用getAll时,它必须是
await getAll()
不需要return
的示例是当您不使用{}
时
const getAll = async() => getPostedPlaces()