使用mongodb x typeormnestjs-创建crud rest api

当试图用"id"按findone()获取数据时.低于错误

TS2345:类型为{id:string;}的参数不可分配给"FindOneOptions"类型的参数.

 const result = await this.sellerRepository.findOne({ id });

实体

export class Seller {
  @ObjectIdColumn()
  id: ObjectID;
  @Column({
    type: 'string',
    nullable: false,
    name: 'product_name',
  })
  productName: string;
  @Column({
    name: 'short_desc',
  })
}


async findOne(id: string): Promise<Seller> {
    const result = await this.sellerRepository.findOne({ id });
    return result;
  }

enter image description here

推荐答案

这是我的解决方案..

我用了new ObjectID(id)

如果你得到像TS7016: Could not find a declaration file for module 'mongodb'这样的误差

然后执行以下步骤

  1. 在根项目中创建任何带有"typings"的文件夹
  2. 创建索引.d、 输电系统
  3. declare module 'mongodb'
  4. 在下面的tsconfig.json文件中添加index.d.ts的路径

"typeRoots": [ "./typings", "./node_modules/@types/" ]

示例代码

import { ObjectID } from 'mongodb';
 async findOne(id: string): Promise<Seller> {
    const result = await this.sellerRepository.findOne(new ObjectID(id));
    return result;
  }

Node.js相关问答推荐

无法验证叶签名|无法验证第一个证书

Firebase-admin筛选器.或只考虑第一个WHERE子句

购物车是空的状态|本地存储不更新产品数量|Redux|

MongoDB如果文档S的字段小于另一个字段,则将该字段加一

Node.js中Redis的并发问题

未显示NPM版本

在内存中加载安全密钥安全吗?还是每次都从文件中读取?

当变量在另一个文件中初始化时,在初始化错误之前无法访问变量

如果我加入另一个公会且我的​​机器人已在其中,欢迎消息发送错误

在 azure blob 容器之间复制文件

在 TypeScript 中正确键入 MongoDB find 方法

只要我在后端正确验证所有内容,就可以将这些数据存储在本地存储中吗?

登录用户并获取他们的个人资料

使用`useLocalStorage`和`useDebounce`时如何解决Next.js中的react-hydration-error

从 CoffeeScript 中的数组中删除一个值

如何在 NodeJs 中下载和解压缩内存中的 zip 文件?

如何在 MongoDB 中查询引用的对象?

node.js 服务器和 HTTP/2 (2.0) 与 express.js

我无法全局安装 nodemon,nodemon无法识别

'node' 未被识别为内部或外部命令