简体   繁体   English

区分 null 和 undefined

[英]Differentiate between null and undefined

I'm working on a GraphQL API project where user is able to update an entire article or portions of it.我正在开发一个 GraphQL API 项目,用户可以在其中更新整篇文章或部分文章。

Usually, I just send JSON with columns I want to update and it works flawlessly for these columns, the json is as follows:通常,我只发送 JSON 以及我想要更新的列,并且它对这些列完美运行,json 如下:

{
    title: "New title",
    categoryId: 3
}

But here's the issue: any other column that I didn't provide in this json becomes a null and simply adding:但问题是:我在此 json 中未提供的任何其他列都变为null并简单地添加:

if(value == null) 
    -- don't update that column

Wouldn't work with columns that can be nullable , because there's no difference between sending:不适用于可以为 nullable的列,因为发送之间没有区别:

{
    title: "New title",
    categoryId: 3
}

and sending并发送

{
    title: "New title",
    categoryId: 3,
    someCol: null
}

Even though in first JSON I want the value to remain unchanged and in second JSON I want it to be set to null.即使在第一个 JSON 我希望值保持不变,在第二个 JSON 我希望它设置为 null。

So, my question is - how can I handle it in EFCore?所以,我的问题是 - 我如何在 EFCore 中处理它?

We have the same situation and we've gone with: null = don't change, anything else = change to value.我们有同样的情况,我们已经走了:null = 不要改变,其他任何东西 = 改变价值。

var entity = ReadEntity();

if(jsonInput.Field1 != null)
{
    entity.Field1 = jsonInput.Field1;
}

This means that you can never 'unset' a field in the entity, but for us, that is acceptable.这意味着您永远不能“取消设置”实体中的字段,但对我们来说,这是可以接受的。 null = uninitialise, once it's initialised, it can't be 'uninitialised', only set to another value (including 'default'). null = uninitialise,一旦初始化,就不能“uninitialised”,只能设置为另一个值(包括“default”)。

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

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