question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to handle undefined values cleanly?

See original GitHub issue

I’m dump ing an object, and fields without values come out like this:

fieldName: !<tag:yaml.org,2002:js/undefined> ''

how can I suppress that to either undefined or just to skip it like JSON.stringify ?

I tried some options from the safeDump

const blob = yaml.dump(obj, { skipInvalid: true, lineWidth: 200 })

https://github.com/nodeca/js-yaml#safedump-object---options-

but without success. I guess I can strip out the undefined fields, but I’d have to do a deep clean of the whole object… also check for false vs undefined etc. etc.

I tried this but it didn’t have any effect: const clean = { ...obj } // remove nulls?

Sure there must be a nicer way to handle this with js-yaml ? Do I have to define my own schema?

related: https://github.com/nodeca/js-yaml/issues/456 https://stackoverflow.com/a/38340374/1146785


  async debugMessage(obj) {
    console.log('json', JSON.stringify(obj, null, 2))
    const blob = yaml.dump(obj)
    console.log('yaml', blob)
  }


// json 

{
  "msg": "router",
  "input": "sleep",
  "eventType": "action",
  "handled": {
    "handled": true,
    "doc": {
      "match": "^cont|.*",
      "goto": "prologue"
    },
    "klass": "room",
    "history": [
      "goto"
    ]
  }
}

// yaml 

msg: router
input: sleep
eventType: action
parsed: !<tag:yaml.org,2002:js/undefined> ''
handled:
  handled: true
  doc:
    match: ^cont|.*
    goto: prologue
  klass: room
  history:
    - goto

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
rlidwkacommented, Dec 18, 2020

Fixed in https://github.com/nodeca/js-yaml/commit/56d5616938af5be943074222b9b10e0519b170b7 (currently in dev branch).

Now undefined follows roughly the same black magic as in JSON:

  • replaced with null in collections (errors out if null isn’t in the schema)
  • removed from mappings entirely if either key or value is undefined

If user defines custom schema for undefined, it serializes as before.

2reactions
NelsonFranciscocommented, Nov 4, 2020

An option would be a nice addition 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to cleanly handle undefined values with a yaml dump ...
I'm dump ing an object, and fields without values come out like this: fieldName: !<tag:yaml.org,2002:js/undefined> ''.
Read more >
7 Tips to Handle undefined in JavaScript
A detailed article about 'undefined' keyword in JavaScript. 7 tips on how to handle correctly 'undefined' and increase code durability.
Read more >
How to treat the undefined values which make sense?
Generally speaking, there are three main approaches to handle missing data: (1) Imputation—where values are filled in the place of missing data, ...
Read more >
Undefined values
A scalar variable (numeric or string) cannot have an 'undefined' value; if an attempt is made to read a variable which has never...
Read more >
How do you deal with null vs undefined?
I consider the difference to be one of implicitly nonexistent (undefined) vs. explicitly having no value (null). A newly declared variable that ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found