3

I'm trying to deploy a simple firebase cloud function using node.js to read a collection, but when deploying it I get this error:

Each then() should return a value or throw promise/always-return

The code is the following

const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);

let db = admin.firestore();

db.collection('collection').get().then((snapshot) => {
  snapshot.forEach((doc) => {
    return console.log(doc.id, '=>', doc.data());
  });
})
.catch((err) => {
  console.log('Error getting documents', err);
});

I tried to add returns but still the error occurs.

return console.log(doc.id, '=>', doc.data());

return console.log('Error getting documents', err);

Kolban
  • 13,794
  • 3
  • 38
  • 60
Francesco
  • 169
  • 1
  • 1
  • 8
  • 1
    Jsut a guess but the first one `then((snapshot) => { ... })` is not returning anything. The return inside the forEach is not the part it's complaining about. – gman Dec 04 '19 at 16:33
  • 1
    Does this answer your question? [Each then() should return a value or throw Firebase cloud functions](https://stackoverflow.com/questions/53354417/each-then-should-return-a-value-or-throw-firebase-cloud-functions) – Kolban Dec 04 '19 at 16:38

2 Answers2

2

You are returning inside a function :) it doesn't count, you must add it here:

db.collection('collection').get().then((snapshot) => {
  return snapshot.forEach((doc) => {
    return console.log(doc.id, '=>', doc.data());
  });
})
.catch((err) => {
  console.log('Error getting documents', err);
});
Kolban
  • 13,794
  • 3
  • 38
  • 60
Renaldo Balaj
  • 2,390
  • 11
  • 23
0

From Documentation: (untested it myself)

return db.collection('collection').get().then(snapshot => {
  snapshot.forEach(doc => {
    return console.log(doc.id, '=>', doc.data());
  });
})
.catch(err => {
  return console.log('Error getting documents', err);
});
sllopis
  • 2,292
  • 1
  • 8
  • 13