gpt4 book ai didi

javascript - 无法在 node-cron 调度方法中获取数据

转载 作者:行者123 更新时间:2023-12-03 22:21:09 24 4
gpt4 key购买 nike

所以我试图在每晚 12 点安排数据库中的更新,并且我能够使用我的服务器运行 Cron 作业,但我无法使用 Axios 获取数据,我尝试了“https”等教程中的外部 URL ://jsonplaceholder.typicode.com/posts/1"并且数据来了,但是当我使用 url/routes 我在 reactjs 前端获取数据时..像这样"http://localhost:9000/api/getAllDealHeader"并将其作为 URL 传递,它会给出类似的错误

Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:183:27) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
config: {
url: 'http://localhost:9000/api/getAllDealHeader',
method: 'get',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.20.0'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
data: undefined
},
request: Writable {
_writableState: WritableState {
objectMode: false,
highWaterMark: 16384,
finalCalled: false,
needDrain: false,
ending: false,
ended: false,
finished: false,
destroyed: false,
decodeStrings: true,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: true,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 0,
prefinished: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object]
},
writable: true,
_events: [Object: null prototype] {
response: [Function: handleResponse],
error: [Function: handleRequestError]
},
_eventsCount: 2,
_maxListeners: undefined,
_options: {
maxRedirects: 21,
maxBodyLength: 10485760,
protocol: 'http:',
path: '/api/getAllDealHeader',
method: 'GET',
headers: [Object],
agent: undefined,
agents: [Object],
auth: undefined,
hostname: 'localhost',
port: '9000',
nativeProtocols: [Object],
pathname: '/api/getAllDealHeader'
},
_ended: true,
_ending: true,
_redirectCount: 0,
_redirects: [],
_requestBodyLength: 0,
_requestBodyBuffers: [],
_onNativeResponse: [Function],
_currentRequest: ClientRequest {
_events: [Object: null prototype],
_eventsCount: 7,
_maxListeners: undefined,
outputData: [],
outputSize: 0,
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_removedConnection: false,
_removedContLen: false,
_removedTE: false,
_contentLength: 0,
_hasBody: true,
_trailer: '',
finished: true,
_headerSent: true,
socket: [Socket],
connection: [Socket],
_header: 'GET /api/getAllDealHeader HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'User-Agent: axios/0.20.0\r\n' +
'Host: localhost:9000\r\n' +
'Connection: close\r\n' +
'\r\n',
_onPendingData: [Function: noopPendingOutput],
agent: [Agent],
socketPath: undefined,
method: 'GET',
path: '/api/getAllDealHeader',
_ended: false,
res: null,
aborted: false,
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object: null prototype]
},
_currentUrl: 'http://localhost:9000/api/getAllDealHeader'
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
timeoutCb: null,
upgradeOrConnect: false,
parser: null,
maxHeadersCount: null,
_redirectable: [Circular],
[Symbol(isCorked)]: false,
[Symbol(outHeadersKey)]: [Object: null prototype]
},
_currentUrl: 'http://localhost:9000/api/getAllDealHeader'
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
我的代码是
const getData = async url => {
try {
const response = await axios.get(url);
const data = response.data;
console.log(data);
} catch (error) {
console.log(error);
}
};


cron.schedule("59 * * * * *", () => {
console.log("running a task every half minute");

let url = 'http://localhost:9000/api/getAllDealHeader'
getData(url);

})
我使用 MySQL 作为我的数据库,并将 sequelize 作为我的 ORM。
有人可以帮助我,我哪里出错了,我该怎么办..

最佳答案

您在函数中使用的变量名称不正确,它是小写的,但您在函数中使用了大写。

     cron.schedule("59 * * * * *",async () => {
console.log("running a task every half minute");

let url = 'https://jsonplaceholder.typicode.com/posts/1'
await getData(url);
})

关于javascript - 无法在 node-cron 调度方法中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63898947/

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