类型的参数' {id:string; }'无法分配到类型的参数' findOneOptions< seller>'
创建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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该使用 findOneby
You should use findOneBy
这是我的上述解决方案。
我使用
new ObjectId(id)
和import {objectId}从'mongodb'';
(如果收到 ts7016:找不到模块'mongodb'的声明文件,
请按照“键入”键入'
声明模块'mongodb'
;index.d.ts
中添加tsconfig.json
文件的路径Typeroots
如下“ Typeroots”:[
。
Here is my solution for above..
I used
new ObjectID(id)
andimport { ObjectID } from 'mongodb';
if you get error like TS7016: Could not find a declaration file for module 'mongodb'
then follow below steps
declare module 'mongodb'
;index.d.ts
intsconfig.json
file undertypeRoots
as below"typeRoots": [ "./typings", "./node_modules/@types/" ]
sample code
您可以使用
fithOneby({id})
或findOne(id)
。您提供的签名与Typeorm提供的任何方法都不匹配,因此这就是Typescript抱怨的原因。您始终可以检查所有可用选项在这里//typeorm.io/“ rel =“ nofollow noreferrer”>在这里。
You can use
fithOneBy({id})
orfindOne(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.
您可以使用“ Typeorm”的导出类型:
...
You can use exported types from 'typeorm' like this:
...
这样使用:
Use it like this: