我遵循了幾個關于如何正確等待來自 ListFile() 函式的資料的指南。我已經在 ListFile() 中將輸出記錄到控制臺,所以我知道資料是正確的。當我嘗試等待函式完成并記錄資料時,我在嘗試等待和承諾時得到兩個輸出之一:
getData error: TypeError: undefined is not an object (evaluating 'response.json')
要么
Promise {
"_U": 0,
"_V": 0,
"_W": null,
"_X": null,
}
我正在創建一個 React Native 應用程式,呼叫 ListFile() 的函式包含一堆其他渲染的組件,因此我無法使整個函式成為異步函式。不知道還能在這里做什么。
呼叫 ListFile()
const getData = async () => {
try {
const response = await ListFile();
const data = response.json();
console.log(data);
} catch (err) {
console.log("getData error: " err);
}
}
getData(); //always returns getData error: TypeError: undefined is not an object (evaluating 'response.json')
串列檔案()
async function ListFile() {
// Reference Firebase Container
var filesInStorageList = [];
let content = [];
const listRef = ref(fbStorage, filePath);
console.log("Listing Files");
// List all files in container
listAll(listRef)
.then((res) => {
res.prefixes.forEach((folderRef) => {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
console.log(res);
});
res.items.forEach((itemRef) => {
// All the items under listRef.
let fileInStorage = itemRef["_location"]["path_"].slice(filePath.length);
filesInStorageList.push(fileInStorage);
// CORRECT DATA IS LOGGED console.log("Pushing " fileInStorage);
// CORRECT DATA IS LOGGED console.log(filesInStorageList);
});
return filesInStorageList;
}).catch((error) => {
// Uh-oh, an error occurred!
console.log("ListFile - ERROR");
});
}
uj5u.com熱心網友回復:
您正在混合 async/await 和 Promises。盡管它們的用途相似,但通常它們是分開使用的,以避免出現這樣的情況,即它們一起使用,如果不知道要尋找什么,則會產生意想不到的結果。
你用 注釋你的ListFile函式async,這意味著它期望找到await某個地方。相反,您使用基于 Promise 的.then和.catch.
要轉換您的代碼,您可以將其更改為:
async function ListFile() {
// Reference Firebase Container
var filesInStorageList = [];
let content = [];
const listRef = ref(fbStorage, filePath);
console.log("Listing Files");
// List all files in container
try {
const res = await listAll(listRef);
//...do your forEach loops
return filesInStorageList;
} catch(err) {
//handle your error
}
}
uj5u.com熱心網友回復:
您應該return在listAll方法上添加一個以在從 呼叫它時回傳其值getData()。請參閱下面的示例代碼:
const getData = async () => {
try {
ListFile()
.then((response) => {
// do something here.
console.log(response);
});
} catch (err) {
console.log("getData error: " err);
}
}
getData();
async function ListFile() {
// Reference Firebase Container
var filesInStorageList = [];
let content = [];
const filePath = "test/";
const listRef = ref(storage, filePath);
// List all files in container
return listAll(listRef)
.then((res) => {
res.prefixes.forEach((folderRef) => {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
console.log(res);
});
res.items.forEach((itemRef) => {
// All the items under listRef.
let fileInStorage = itemRef["_location"]["path_"].slice(filePath.length);
filesInStorageList.push(fileInStorage);
// CORRECT DATA IS LOGGED console.log("Pushing " fileInStorage);
// CORRECT DATA IS LOGGED console.log(filesInStorageList);
// console.log(filesInStorageList);
});
return filesInStorageList;
}).catch((error) => {
// Uh-oh, an error occurred!
console.log(error);
});
}
此外,正如@jnpdx 所說,您必須在您的ListFile()
uj5u.com熱心網友回復:
我進行了 jnpdx 建議的更改,并修改了我的 getData:
const getData = async () => {
try {
ListFile()
.then((response) => {
console.log("Response: " response);
});
} catch (err) {
console.log("getData error: " err);
}
}
getData();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445038.html
標籤:javascript 火力基地 反应式 异步等待 承诺
上一篇:元素型別無效預期字串反應本機移動
