【发布时间】:2019-10-19 16:39:22
【问题描述】:
我正在使用 Express、Typescript 和 MongoDB Node.JS 驱动程序来开发我的 API;目前我遇到了 findOne 方法的问题,因为返回了一个承诺,但是,只有第一个文档的对象与查询匹配,或者如果出现问题,则返回一个简单的 null。
所以我试图在 Promise 得到解决时将该对象放入一个变量中,所以我有这样的东西:
let user = await db.collection('collectionName').findOne({ email: "example@stack.com" });
所以当我尝试为我的变量设置接口时,我收到下一个错误:
let user: UserStructure = await db.collection('collectionName').findOne({ email: "example@stack.com" });
// Type 'object | null' is not assignable to type 'UserStructure'.
// Type 'null' is not assignable to type 'UserStructure'.
一些建议?
【问题讨论】:
-
let user: UserStructure | null = ...?
标签: node.js mongodb typescript express