[英]Storing data from axios to use it later via util functions
我有一個簡單的 axios GET 調用,我想將其存儲起來以供某些 util 函數以后使用。 由於這是一個反應應用程序,它可以存儲在內存(瀏覽器)或目錄中的文件中嗎?
const data = [];
const getData = (logName) => {
if (logName === '') {
alert('Please enter the name of your log!');
} else {
axios({
method: 'GET',
url: `http://xxx-yyy-zzz/debugger/RTS/${logName}/logs`,
})
.then((res) => {
alert('Successfully Imported');
return data.push(res.data);
})
.catch((err) => {
console.log(err);
});
}
};
const askPrice = () => {
return data.map((result) => result.request.requestContext.mpcBidRequest.askPrice);
};
這是我目前擁有的,以前的data
只是import data from '../data/x.json';
但是現在我想調用請求並替換import data from '../data/x.json';
使用從 GET 響應返回的數據。
功能...
const askPrice = () => {
return data.map((result) => result.request.requestContext.mpcBidRequest.askPrice);
};
稍后將使用(在調用 API 之后)然后映射 JSON 對象數組以將結果輸出到 UI。 我的應用程序使用import data from '../data/x.json';
運行良好import data from '../data/x.json';
但現在我想獲取用戶輸入來生成數據。
我已經設法將我需要的東西存儲到一個let
並測試了一些東西......一切正常。
let data = [];
async function getData(filter, logName) {
if (logName === '') {
alert('Please enter the name of your log!');
} else {
axios({
method: 'GET',
url: `http://xxx/debugger/${filter}/${logName}/logs`,
})
.then((res) => {
if (res.data.length === 0) {
console.log(res.data);
alert('Your query returned empty JSON!');
} else {
console.log(res.data);
data = res.data;
}
return res.data;
})
.catch((err) => {
console.log(err);
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.