Javascript 无法在节点cron计划方法内获取数据
因此,我试图在每晚12点在数据库中安排一次更新,并且我能够使用我的服务器运行Cron作业,但是我无法使用Axios获取数据,我尝试了一个来自教程的外部URL,如“https://jsonplaceholder.typicode.com/posts/1“数据来了,但是当我使用我创建的url/路由在reactjs前端获取数据时。。像这样“http://localhost:9000/api/getAllDealHeader“并将其作为URL传递,它会给出如下错误Javascript 无法在节点cron计划方法内获取数据,javascript,node.js,reactjs,sequelize.js,Javascript,Node.js,Reactjs,Sequelize.js,因此,我试图在每晚12点在数据库中安排一次更新,并且我能够使用我的服务器运行Cron作业,但是我无法使用Axios获取数据,我尝试了一个来自教程的外部URL,如“https://jsonplaceholder.typicode.com/posts/1“数据来了,但是当我使用我创建的url/路由在reactjs前端获取数据时。。像这样“http://localhost:9000/api/getAllDealHeader“并将其作为URL传递,它会给出如下错误 Error: read ECONNRE
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);
})嗯,我的错,但我的代码中的名称是正确的,这不是问题所在。……你好,阿西姆,你还记得你是如何解决这个案子的吗?非常感谢。