Google Cloud Function asyn c с несколькими запросами на выборку - PullRequest
0 голосов
/ 07 августа 2020

Я новичок в GCF и Javascript asyn c и борюсь с этим. Сначала я выполняю вызов fetch, а затем передаю этот ответ в качестве параметра второй функции, которая затем также выполняет отдельный вызов fetch.

Во время второй функции мой пустой инициализированный json получает свойства, добавленные к нему, и когда эта функция завершится, я хочу уведомить exports.helloHttp, чтобы затем выполнить res.end и завершить работу.

Я пробовал связать дополнительный пустой then(), но, похоже, он не работает.

Мой код:

var json = {}; // <- gets properties added to it during secondFunction()

exports.helloHttp = (req, res) => {
  fetch("firstfetchurl.com",requestOptions)
  .then(result => result.json())
  .then(response => {
    // next take the result and create a new product
    return secondFunction(response);
  })
  .catch(error => console.log('error', error));

  // res.end(JSON.stringify(json)); <- this is what I want my cloud function to output, but only after secondFunction completes        
};

Ответы [ 2 ]

1 голос
/ 07 августа 2020

Вот код, который будет делать то, что вы хотите (заменить URL-адреса выборки и установить соответствующие параметры)

const fetch = require('node-fetch');

exports.helloHttp = async (req, res) => {
    return fetch("https://jsonplaceholder.typicode.com/users/1/albums") // First fetch
        .then(firstFetchResponse => firstFetchResponse.json())
        .then(firstFetchResponse => secondFunction(firstFetchResponse)) // Second fetch
        .then(secondFunctionResponse => secondFunctionResponse.json())
        .then(finalResponse => res.json(finalResponse)) // This line sends your response to the client
        .catch(error => { console.error('Error', error); res.status(500).send('Server Error') }); // In case an error, log and send an error response
};

async function secondFunction(data) {
    // Logic of your second function. Here just does another fetch using the data from the first request
    let firstAlbumId = data[0].id
    return fetch(`https://jsonplaceholder.typicode.com/albums/${firstAlbumId}/photos`);
}

Эта же функция может использовать await как это

exports.helloHttp = async (req, res) => {
    try {
        let response = await fetch("https://jsonplaceholder.typicode.com/users/1/albums") // Note the await on this line
            .then(result => result.json())
            .then(firstFetchResponse => secondFunction(firstFetchResponse))
            .then(secondFetchResponse => secondFetchResponse.json());
        res.json(response); // Finally you are sending the response here.
    } catch (error) {
        console.error(error);
        res.status(500).send('Server Error');
    }
};

Наконец, вам также необходимо убедиться, что package.json имеет зависимость для node-fetch

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "node-fetch": "^2.6.0" // This line must be there
  }
}

Для отправки ответа JSON он использует этот метод .

0 голосов
/ 07 августа 2020

result.json() не является асинхронной операцией, поэтому вам не нужно использовать блок then(). Следующее должно помочь:

exports.helloHttp = (req, res) => {
   fetch("firstfetchurl.com",requestOptions)
   .then(result => {
     return secondFunction(result.json());
   })
   .catch(error => console.log('error', error));
//...

Обратите внимание, что, в зависимости от конкретной цели вашей функции helloHttp, вам может потребоваться вернуть всю цепочку обещаний, как показано ниже:

exports.helloHttp = (req, res) => {
   return fetch("firstfetchurl.com",requestOptions)  // Note the return
   .then(result => {
     return secondFunction(result.json());
   })
   .catch(error => console.log('error', error));
//...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...