Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 理解异步函数-->;第二个函数即将运行_Node.js_Async Await_Fetch_Fetch Api - Fatal编程技术网

Node.js 理解异步函数-->;第二个函数即将运行

Node.js 理解异步函数-->;第二个函数即将运行,node.js,async-await,fetch,fetch-api,Node.js,Async Await,Fetch,Fetch Api,我进行API调用以获取第二个函数所需的令牌。但是这个很快就会运行,所以没有数据。(我得到一个错误,json有一个意外的结尾)。之后我打了4个电话。我在谷歌应用程序脚本中做了这个,但是这个洞脚本大约需要17分钟。所以我想在NodeJS中创建它并部署在firebase上,然后为apps脚本创建一个端点以获取已经准备好的数据 第一个功能 const express = require('express'); const app = express(); const fetch = require('n

我进行API调用以获取第二个函数所需的令牌。但是这个很快就会运行,所以没有数据。(我得到一个错误,json有一个意外的结尾)。之后我打了4个电话。我在谷歌应用程序脚本中做了这个,但是这个洞脚本大约需要17分钟。所以我想在NodeJS中创建它并部署在firebase上,然后为apps脚本创建一个端点以获取已经准备好的数据

第一个功能

const express = require('express');
const app = express();
const fetch = require('node-fetch'); 
const resid = require('./calls.js');


/*
Here we start with the request to get a new token. And pass this on in the request's later on.
*/
async function getToken() {

    const config = {
        method: 'post',
        headers: {}
    };

    const url = 'https://theapirurl.com';

    const response = await fetch(url, config);
    const json = await response.json();
    const token = json['access_token'];

    await resid.getReservationIds(token);

}

getToken()
第二个函数(在calls.js上)

我做错了什么

编辑

现在我得到了这个错误:

(node:43371) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://apiurlfromcallsjs.com reason: Unexpected end of JSON input
    at /Users/remco/functions/node_modules/node-fetch/lib/index.js:272:32
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async getReservationIds (/Users/remco/functions/calls.js:20:18)
(node:43371) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:43371) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:43371) UnhandledPromiseRejectionWarning: TypeError: getReservationIds is not a function
    at getToken (/Users/remco/functions/index.js:23:11)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:43371) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)

您正在立即调用第二个函数,而不是等待第一个函数完成:

module.exports = {
    resid: getReservationIds()
}
所以当你跑步的时候

const resid = require('./calls.js');
此时,第二个函数已经在运行中(并且
resid.getReservationId
是一个承诺,而不是一个函数)

只导出函数:

module.exports = getReservationIds;
然后导入并调用它:

const getReservationIds = require('./calls.js');
// ...
  await getReservationIds(token);

您正在立即调用第二个函数,而不是等待第一个函数完成:

module.exports = {
    resid: getReservationIds()
}
所以当你跑步的时候

const resid = require('./calls.js');
此时,第二个函数已经在运行中(并且
resid.getReservationId
是一个承诺,而不是一个函数)

只导出函数:

module.exports = getReservationIds;
然后导入并调用它:

const getReservationIds = require('./calls.js');
// ...
  await getReservationIds(token);

嗨@CertainPerformance现在我发现一个错误,我贴在了原来的帖子里。但是你上面的awnser是有道理的,谢谢!当我在calls.js中将令牌作为常量传递时,它就像一个符咒。因此,据我所知,当GetReservationId运行时,令牌是未定义的。如果未定义
令牌
,则
json['access\u token']
中不存在任何内容。检查响应以找出获取令牌所需的正确属性。还请注意,要设置授权标头,您需要在令牌前的
Bearer
后面留一个空格,也就是说,您可能需要
“authorization”:“Bearer”+token
当我在调用getReservationId之前记录令牌时,我首先得到上面的错误,然后是终端中的令牌。(令牌是正确的并且正在工作)因此我认为getReservationId在getToken完成之前正在运行。就像我说的。如果我在getReservationId函数中以常量形式传递令牌,并且do:node calls.js it worksHi@CertainPerformance,那么现在我会收到一个错误,我会粘贴到原始帖子中。但是你上面的awnser是有道理的,谢谢!当我在calls.js中将令牌作为常量传递时,它就像一个符咒。因此,据我所知,当GetReservationId运行时,令牌是未定义的。如果未定义
令牌
,则
json['access\u token']
中不存在任何内容。检查响应以找出获取令牌所需的正确属性。还请注意,要设置授权标头,您需要在令牌前的
Bearer
后面留一个空格,也就是说,您可能需要
“authorization”:“Bearer”+token
当我在调用getReservationId之前记录令牌时,我首先得到上面的错误,然后是终端中的令牌。(令牌是正确的并且正在工作)因此我认为getReservationId在getToken完成之前正在运行。就像我说的。如果我在getReservationId函数中以常量形式传递令牌,并执行do:node calls.js,它就会工作