首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在node-cron调度方法中获取数据

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

Stack Overflow用户
提问于 2020-09-15 17:27:58
回答 1查看 345关注 0票数 1

所以我试着每天晚上12点在数据库中安排一次更新,我可以使用我的服务器运行Cron作业,但我不能使用Axios获取数据,我尝试了一个来自教程的外部URL,比如"https://jsonplaceholder.typicode.com/posts/1“,数据来了,但是当我使用我创建的url/路由在reactjs前端获取数据时。类似于这个"http://localhost:9000/api/getAllDealHeader“,并将其作为URL传递,它会给出如下错误

代码语言:javascript
复制
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]
}

我的代码是

代码语言:javascript
复制
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。有人能帮帮我吗?我哪里出错了?我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2020-09-15 18:34:11

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

代码语言:javascript
复制
     cron.schedule("59 * * * * *",async () => {
   console.log("running a task every half minute");

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

})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63898947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档