gpt4 book ai didi

javascript - 增加 React 中的超时时间 - neo4j 应用程序

转载 作者:行者123 更新时间:2023-11-30 14:31:03 25 4
gpt4 key购买 nike

我正在尝试增加我的 React 应用程序的超时时间。我正在使用 axios,所以最初我尝试了:

axios.post('/gene_info', postData, {timeout: timeoutVal});

它没有工作,并且有相应的线程处理它:

https://github.com/axios/axios/issues/647

所以,我尝试了以下代码:

 let CancelToken = axios.CancelToken;
const source = CancelToken.source();
try {
let response = null;
setTimeout(() => {
if (response === null) {
source.cancel();
}
}, 60 * 1500 * 1000);

response = await axios.post('/gene_info', postData, {cancelToken: source.token});
console.log(response);
} catch (error) {
console.log(error);
}

而且它也不起作用。请求超时,我看到空响应错误,即使在 Node.js 后端我看到结果已正确返回。在后端,我对 Neo4j 数据库发出了一个运行时间很长的请求。我怀疑它可能会超时,所以我在 neo4j.config 文件中添加了以下行:

unsupported.dbms.executiontime_limit.enabled=true
unsupported.dbms.executiontime_limit.time=99999999999999s

我在这里找到的:

How to configure a query timeout in Neo4j 3.0.1

并重新启动了 neo4j 但它也没有帮助。这是我在终端中看到的:

enter image description here

我不确定这个POST/gene_info - - ms - -是什么意思,问题是出在前端,还是后端,但我怀疑 neo4j 现在超时了,但它仍在计算我使用 console.log() 语句看到的结果。任何建议将不胜感激。

Update

我尝试使用 Reacts fetch,但仍然无法正常工作。这是代码:

fetchWithTimeout = (url, postData, timeout) => {
let didTimeOut = false;

new Promise(function(resolve, reject) {
const timeout = setTimeout(function() {
didTimeOut = true;
reject(new Error('Request timed out'));
}, timeout);

fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
timeout: timeout,
body: JSON.stringify(postData)
})
.then(function(response) {
// Clear the timeout as cleanup
clearTimeout(timeout);
if(!didTimeOut) {
console.log('fetch good! ', response);
resolve(response);
}
})
.catch(function(err) {
console.log('fetch failed! ', err);

// Rejection already happened with setTimeout
if(didTimeOut) return;
// Reject with error
reject(err);
});
})
.then(function() {
// Request success and no timeout
console.log('good promise, no timeout! ');
})
.catch(function(err) {
// Error: response error, request timeout or runtime error
console.log('promise error! ', err);
});
}

然后我这样调用这个函数:

let postData = {"jsonData": geneNameArr,
"datasetName": this.props.datasetName};
this.fetchWithTimeout('/gene_info', postData, timeout).then((response) => {
console.log("fetchWithTimeout is done!");
console.log(response);
});

Update

我尝试使用 axios.create() 函数但没有成功:

  const axiosInstance = axios.create({
baseURL: '/gene_info',
timeout: timeout
});

axiosInstance.post('', postData).then((response) => {
console.log("axios request is done with create() method");
console.log(response);
});

如果前端似乎没有任何工作,我会认为这是来自 neo4j 驱动程序的超时,即使以某种方式返回了结果。这是我为驱动程序使用的代码:

router.post('/gene_info', function(req, res) {
...
...
var driver = dbUtils.driver;
const session = driver.session();
session.run(
full_query,
{}
).then(result => {
const exprData = chartService.prepareGeneInfoData(result, '');
res.json({
exprData
});

session.close();
});
})

或者它也可以是 express.Router(); 我正在使用它来处理后端的 getpost 请求Node.js

最佳答案

如果你想在axios中配置你的超时时间,你可以使用,

const axiosInstance = axios.create({
baseURL: "http://example.com/api/",
timeout: 5000
});

5000 替换为您需要的超时值。

关于javascript - 增加 React 中的超时时间 - neo4j 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51159966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com