简体   繁体   English

在 Nodejs + express API 中指定请求/响应模式

[英]Specify Request/Response Modals in Nodejs + express APIs

I've created an API in nodejs + express => update user API where data comes in body我在 nodejs + express => 更新用户 API 中创建了一个 API ,其中数据进入正文

router.patch('/profile', verifyAuth, updateProfile);

In updateProfile using const reqData = req.body I'm updating the reqData in user modal在使用const reqData = req.body的 updateProfile 中,我正在更新用户模式中的 reqData

data such as:数据,例如:

{
 firstName: string,
  lastName: string,
  gender: string,
  dateOfBirth: Date,
  email: string,
  phone: string,
}

Here phone number is my unique key so, I don't want anyone to update phone and there are many other keys as well.这里电话号码是我唯一的钥匙,所以我不希望任何人更新电话,还有很多其他的钥匙。

Therefore I want to capture only fields which I want to update in req.body for update profile API.因此,我只想捕获我想在 req.body 中更新的字段以更新配置文件 API。

Is there a way to specify request modal for APIs in express nodejs?有没有办法在 express nodejs 中为 API 指定请求模式? If someone add any other keys in request it will be neglected in req.body如果有人在请求中添加任何其他键,它将在 req.body 中被忽略

I think it will help you:) To skip the update of th phone number, you just have to skip the key "phone" in the dictionnary.我认为它会对您有所帮助:) 要跳过电话号码的更新,您只需跳过字典中的“电话”键。

Have a nice day.祝你今天过得愉快。

 function checkProfile(item){ return { firstName: item.firstName, lastName: item.lastName, gender: item.gender, dateOfBirth: item.dateOfBirth, email: item.email, phone: item.phone, } } const rawProfile = { firstName: "string", lastName: "string", gender: "string", dateOfBirth: new Date(), email: "string", phone: "string", additionalValue1:"xxxxxx", additionalValue2:"xxxxxx", additionalValue3:"xxxxxx", } console.log(checkProfile(rawProfile))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM