类型的参数' {id:string; }'无法分配到类型的参数' findOneOptions< seller>'

发布于 2025-02-11 22:53:28 字数 1015 浏览 1 评论 0原文

创建crud rest api

使用 mongodb typeorm nestjs -在尝试通过 findOne()获取数据时 id '。以下错误

ts2345:类型'{id:string的参数; }'不能分配给类型“ FindOneOptions”的参数。
对象文字只能指定已知属性,并且“ id”中不存在“ findOneOptions”中的“ id”。

代码:

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

实体

@Entity('seller')
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;
  }

”在此处输入图像描述”

Using mongodb by typeorm with nestjs - create crud rest api

When trying to get data by findone() with 'id' . getting below error

TS2345: Argument of type '{ id: string; }' is not assignable to parameter of type 'FindOneOptions'.
Object literal may only specify known properties, and 'id' does not exist in type 'FindOneOptions'.

Code:

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

Entity

@Entity('seller')
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不弃不离 2025-02-18 22:53:28

您应该使用 findOneby

findOne(id: number): Promise<User> {
   return this.usersRepository.findOneBy({ id: id });
}

You should use findOneBy

findOne(id: number): Promise<User> {
   return this.usersRepository.findOneBy({ id: id });
}
故事和酒 2025-02-18 22:53:28

这是我的上述解决方案。

我使用new ObjectId(id)
import {objectId}从'mongodb'';

(如果收到 ts7016:找不到模块'mongodb'的声明文件,

请按照“键入”键入'

  1. 键入'' mongodb' 'root项目
  2. 创建index.d.ts
  3. 添加声明模块'mongodb';
  4. index.d.ts中添加tsconfig.json文件的路径
    Typeroots如下

“ Typeroots”:[

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

Here is my solution for above..

I used new ObjectID(id) and
import { ObjectID } from 'mongodb';

if you get error like TS7016: Could not find a declaration file for module 'mongodb'

then follow below steps

  1. create folder with 'typings'any at root project
  2. create index.d.ts
  3. add declare module 'mongodb';
  4. add path of the index.d.ts in tsconfig.json file under
    typeRoots as below

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

sample code

import { ObjectID } from 'mongodb';
 async findOne(id: string): Promise<Seller> {
    const result = await this.sellerRepository.findOne(new ObjectID(id));
    return result;
  }
瑕疵 2025-02-18 22:53:28

您可以使用fithOneby({id})findOne(id)。您提供的签名与Typeorm提供的任何方法都不匹配,因此这就是Typescript抱怨的原因。

您始终可以检查所有可用选项在这里//typeorm.io/“ rel =“ nofollow noreferrer”>在这里

You can use fithOneBy({id}) or findOne(id). The signature you provided does not match any method provided by TypeORM, so that's why Typescript complains.

You can always check all available options here and here.

兔姬 2025-02-18 22:53:28

您可以使用“ Typeorm”的导出类型:

import { FindOptionsWhere } from 'typeorm';

...

const result = await this.sellerRepository.findOne({ id: id as FindOptionsWhere<Seller> });

You can use exported types from 'typeorm' like this:

import { FindOptionsWhere } from 'typeorm';

...

const result = await this.sellerRepository.findOne({ id: id as FindOptionsWhere<Seller> });
束缚m 2025-02-18 22:53:28

这样使用:

const prodId = await this.productEntity.findOne({
  where: {
    column_name_in_Entity: defined initializer
  }
})

Use it like this:

const prodId = await this.productEntity.findOne({
  where: {
    column_name_in_Entity: defined initializer
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文