Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在某些字段中做一个例外_Javascript_Mongodb_Typescript_Mongoose - Fatal编程技术网

Javascript 在某些字段中做一个例外

Javascript 在某些字段中做一个例外,javascript,mongodb,typescript,mongoose,Javascript,Mongodb,Typescript,Mongoose,我有一个方法可以使用.findOneAndUpdate AuthorInterface如下所示: 导出接口AuthorInterface{ 名称:string, 生物:字符串, githubLink:string, stackoverflowLink:string, twitterLink:string, 图像:字符串, image_webp:string, } 在这种情况下,我需要在这里破例图像和图像_webp是图像路径,因此我不能只覆盖这些值。我已经使用了ommit将其从AuthorInt

我有一个方法可以使用
.findOneAndUpdate

AuthorInterface
如下所示:

导出接口AuthorInterface{
名称:string,
生物:字符串,
githubLink:string,
stackoverflowLink:string,
twitterLink:string,
图像:字符串,
image_webp:string,
}
在这种情况下,我需要在这里破例<代码>图像和
图像_webp
是图像路径,因此我不能只覆盖这些值。我已经使用了
ommit
将其从
AuthorInterface
参数中删除

然而,typescript仍然在
作者:{…作者}
抱怨
image\u webp
image
字段缺失。如何告诉typescript我不希望参数对象中有
image
image\u webp
属性

public saveAuthor(_id:mongoose.Types.ObjectId | string,author:Omit):承诺{
返回User.findOneAndUpdate({u id},{author:{…author}},{new:true}).exec()
}

像这样更改您的
作者界面

导出接口AuthorInterface{
名称:string,
生物:字符串,
githubLink:string,
stackoverflowLink:string,
twitterLink:string,
图像?:字符串,
image_webp?:字符串,
}

这就是打字脚本的美妙之处

public saveAuthor(_id: mongoose.Types.ObjectId | string, author: unknown): Promise<UserModelInterface | null> {
    return User.findOneAndUpdate({ _id }, { author: author as AuthorInterface }, { new: true }).exec()
}
public saveAuthor(_id:mongoose.Types.ObjectId | string,author:unknown):承诺{
返回User.findOneAndUpdate({u id},{author:author as AuthorInterface},{new:true}).exec()
}
以下是我的解决方案:

从“猫鼬”导入猫鼬
界面作者界面{
名称:string,
生物:字符串,
githubLink:string,
stackoverflowLink:string,
twitterLink:string,
图像:字符串,
image_webp:string,
}
键入AuthorUpdate=Omit;
/**
*我敢打赌,你可以更改/更新/修改
*用户对象/类
* 
*以下是您应该做的:
*/
界面用户{
findOneAndUpdate:(arg1:any,author:AuthorUpdate扩展T?AuthorUpdate:T,arg3:any)=>any
}
/**
*用户模拟,仅用于测试目的
*/
常量用户:用户={
findOneAndUpdate:(…args:any[])=>{}
}
/**
*现在,如果您将AuthorUpdate传递给findOneAndUpdate,它将按预期工作
*/
函数saveAuthor(_id:mongoose.Types.ObjectId | string,author:AuthorUpdate){
返回User.findOneAndUpdate({u id},{author:{…author}},{new:true}).exec()
}
/**
*如果您对我的解决方案有疑问,请分享最小可复制示例
*/

对于您的
saveAuthor
函数,该函数仅使用
author
克隆并传递给
User.findOneAndUpdate

为了非常直观,您可以键入
saveAuthor
的参数,如下所示:

author: Parameters<(typeof User)['findOneAndUpdate']>[1].author
但是你正在尝试删除属性。。你的这句话听起来很奇怪:

image
image\u webp
是图像路径,因此我不能只覆盖这些值”

但是您不能在
saveAuthor
中修改这些属性。您是否担心
User.findOneAndUpdate
修改属性?这应该无关紧要,因为您正在使用扩展运算符克隆对象:
{…author}

听起来你可能把类型和值混淆了。使用
省略
不会从实际值中删除属性;它只放宽参数的类型要求。在大多数情况下,您仍然可以使用
image
image\u webp
传入对象(除非您正在编写文字对象,否则typescript的“多余属性检查”启发式不会生效)

您似乎误解了
author:{…author}
处的typescript错误。您会说“我如何告诉typescript我不希望这些属性出现在这个对象中?”但错误并不是关于
saveAuthor
函数所期望的,而是关于
User.findOneAndUpdate
所期望的。您向它传递了一个有缺陷的
作者界面
(不是完整的
作者界面
),但它需要一个完整的
作者界面

如果
User.findOneAndUpdate
要求太多,则可以将其更改为放宽其参数类型。如果要删除这些值,请执行以下操作:

author: {
    ...author,
    image: undefined,
    image_webp: undefined,
}

如果
User.findOneAndUpdate
的类型不接受此选项,则使用nancy的答案进行修复,假设代码支持此选项。

我可以,但我实际上也在其他地方使用AuthorInterface。因此,我不想简单地设置它optional@Ifaruki既然您不想更改
AuthorInterface
,为什么不将
image
image\u webp
设置为
'
,而不是从对象中删除这些属性呢?禁用typescript的好处
author: {
    ...author,
    image: undefined,
    image_webp: undefined,
}