Francesco
Francesco

Reputation: 179

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

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);

Upvotes: 3

Views: 934

Answers (2)

sllopis
sllopis

Reputation: 2368

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);
});

Upvotes: 0

Renaldo Balaj
Renaldo Balaj

Reputation: 2440

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);
});

Upvotes: 2

Related Questions