node.js - mongodb typescript 类型检查
问题描述
在节点中,我使用的是 mongodb 客户端,findMany 查询返回一个文档数组。
我真正需要的是我应该能够确保返回的 Array 是否包含返回类型所期望的所有字段。
我已经定义了一个类型,例如游戏
export type Game {
name: string;
description: string;
...
}
我的查询看起来像
const games = (await this.games.find({}).toArray()) as Game[];
但是我没有找到任何机制来确定响应是否包含Game
类型所需的所有字段。
解决方案
您可以确保返回数组上的文档包含所有使用$exists
.
const games = (await this.games.find({field1: {$exists: true}, field2: {$exists: true}}).toArray()) as Game[];
或使用 Game 的按键使其充满活力。
推荐阅读
- python - 使用 FFT 求解 PDE(Cahn Hilliard 方程)
- git - 八达通部署与来自 Git 的 Terraform 模块
- batch-file - 批处理语言taskkill命令
- tensorflow - 如何添加密集层的输出
- python - 如何在python中合并文本文件中的某些行
- java - 由 puppeteer 控制的 JCEF
- reactjs - 我需要使用 Mongoose 来搜索不重叠的参数吗?
- python - 如何使用 openpyxl 正确读取 excel 文件?
- html - 如何将响应式嵌入式 youtube iframe 居中?
- reactjs - 在 JSS 中使用关键帧