我从FireStore获取数据,可以打印doc.data,它提供了一个对象,其中包含集合中的值列表,其中之一是unixtime.

问题:我不能只打印unixTime的特定值,它是未定义的,所以我试图弄清楚如何正确地应用我的类型模型,这样我就可以通过控制台记录unixTime的值.我的类型模型实现做错了什么?如何只打印unixtime的值?第一张照片是Firestore中的数据,第二张照片是doc.log云函数中的数据打印.第三张照片是,如果我将myVar2添加到doc.data的末尾,它会显示"doc.data unfined".

import { firestore } from 'firebase-admin';

export default async () => {
  const FirestoreInstance = firestore();
  const getunixtime = FirestoreInstance.collection('2023WNS').doc('uDIP3FvGg2bxDbBusJ9cq4iIyec2');
  const doc = await getunixtime.get();

  type ObjectKey2 = keyof typeof doc.data;
  const myVar2 = 'unixtime' as ObjectKey2;

  if (!doc.exists) {
    console.log('No such document!');
  } else {
    console.log('Document data:', doc.data[myVar2]);
  }
}

enter image description here

enter image description here

enter image description here

下面的 comments 中建议了这部剧的变化.

enter image description here

推荐答案

doc.data()是一个返回对象而不是属性的函数.你所需要做的就是:

const data = doc.data();
const unixtime = data.unixtime;

这与打字或打字信息无关.您只是没有调用data方法来获取文档数据.

Node.js相关问答推荐

postgresql层与应用层的序列化

当FastifyJS向客户端发送响应时,apache 不会将其发送给他

当API返回400状态代码时,使用Reactjs fetch获取错误消息

Redis Typescript 删除方法类型转换

Promise 和 Azure 语音转文本

Fly.io 启动问题:无法从源获取图像或构建错误:构建错误:连接期间出错:发布...

ResponseError:键空间ks1不存在

如何为单个 mongo nodejs 驱动程序找到最佳连接池大小

npm chokidar 触发事件两次

Express.js cookie setter 的 Domain 属性:如何与 *多个 * 域共享 cookie?

如何在不使用位置运算符 $ 的情况下更新 mongodb 文档中数组中的嵌套文档?

多个 Axios 请求合并

在 Passport 策略回调中获取请求对象

file.slim.js 中的苗条是什么

Connect/Express 中的session和cookieSession中间件有什么区别?

JSHint 是否支持异步/等待?

如何在不全局安装的情况下在 Node REPL 中要求 node 模块?

从 React(同构应用程序)进行 API 调用时出现Access-Control-Allow-Origin问题

未在 Windows 8.1 上构建的 node 包 - 缺少 Microsoft.Cpp.Default.props

NPM:为什么要安装这个包?