无法使用 admin sdk 获取 firestore 文档

Jus*_*ung 0 javascript firebase google-cloud-functions

我有一些测试文件试图使用 admin sdk 获取 firestore 文档。我不断收到错误TypeError: admin.firestore(...).collection(...).doc(...).then is not a function

我在本地服务/测试。我究竟做错了什么?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({
  credential: admin.credential.cert(functions.config().cert),
  databaseURL: functions.config().databaseURL
});

const getMessage = () => {
  return admin.firestore().collection('messages').doc('1DwcY72Jt4Wj9QuZ8TyR').then(res => console.log(res)).catch(err => {
    return err
  })
}

getMessage();
Run Code Online (Sandbox Code Playgroud)

imj*_*red 5

我认为您需要对所需的文档执行一些操作。尝试.get()

https://firebase.google.com/docs/firestore/query-data/get-data#g​​et_a_document

所以对你来说:

return admin
  .firestore()
  .collection('messages')
  .doc('1DwcY72Jt4Wj9QuZ8TyR')
  .get()
  .then(res => console.log(res)).catch(err => {
Run Code Online (Sandbox Code Playgroud)