Вы не передали обработчик catch должным образом.
Измените это:
app.get('/post', (req, res) => {
readFirestore('fake',null).then(response => {
res.json(response);
}),(err) => {
console.log(err);
res.send(err);
}
});
на следующее:
app.get('/post', (req, res) => {
readFirestore('fake',null).then(response => {
res.json(response);
}, (err) => {
console.log(err);
res.send(err);
});
});
Кроме того, readFirestore()
не нужно создать собственное обещание (это анти-шаблон). Он может просто вернуть обещание, которое у вас уже есть.
Вот реструктурированная readFirestore()
функция:
function readFirestore(collection, doc) {
//If doc param is null Query collection
if (!doc) {
return db.collection(collection).get().then((snapshot) => {
const response = [];
snapshot.forEach((doc) => {
response.push(doc.id);
});
return response;
}).catch((err) => {
// log the error, then rethrow
// or, if you don't need to log the error here, then just get rid of the .catch()
// here entirely as it doesn't change your logic flow, it's only here for logging
console.log('Error getting documents: ', err);
throw err;
});
} else {
// it's unclear what you want returned here, but you need to return a promise
// that resolves to something - you need to fill in this logic
return Promise.resolve(null);
}
}