在 Node 后端写接口时,可以通过判断前端传入的参数值是否为 null 或者 undefined 来确定字段是否需要被置为空。
例如,在使用 Express 框架编写 RESTful API 时,可以使用 req.body 对象获取前端传来的请求体数据。假设前端传来的请求体中有一个字段叫做 name,则可以这样判断:
if (req.body.name === null || req.body.name === undefined) {
// 如果 name 值为 null 或 undefined,则将其赋值为空字符串
req.body.name = '';
}
然后再根据业务需求进行相应处理即可。