Home > Software design >  axios is returnig error 400 when testing the function on firebase
axios is returnig error 400 when testing the function on firebase

Time:01-21

I want to update a field in the firebase db based on an api response, but when I test the function in google cloud i get a 400 error. Not sure what do I miss:

import functions from 'firebase-functions';
import admin from 'firebase-admin';
import axios from 'axios';
const { initializeApp, credential: _credential, firestore} = admin;
admin.initializeApp();

const db = firestore();  

async function UpdateCountField() {
  console.log("Updating count task start point.");
  const query = db.collection('users').where('count', '==', '');
  return query.get().then(async snapshot => {
    console.log(query.get('erd'));
    let resptoken =  await axios.get(
        `https://api/${query.get('erd');
      result = resptoken.data;
    const promises = snapshot.docs.map(doc => doc.ref.update({ 'count': '${result}' }));
    return Promise.all(promises)
  })
  }

CodePudding user response:

managed in the end with a much simpler approach

const query = db.collection('users').where('count', '==', '').where('nou', '==', false).where('erd','!=','').get();
  const documents = (await query).docs;
  if ( documents.count == 0) {
    return;
  }
  for(const doc of documents) {
    let result = 0;
    try {
      let url = `https://api.../${doc.get('projectID')}`;
      let raspuns = await axios.get(url);
}
}
  • Related