Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

marshal fails on null values #22

Closed
yusefnapora opened this issue Oct 13, 2016 · 0 comments · Fixed by #26
Closed

marshal fails on null values #22

yusefnapora opened this issue Oct 13, 2016 · 0 comments · Fixed by #26

Comments

@yusefnapora
Copy link
Contributor

Trying to marshal an object that contains a null value results in a TypeError:

ipld.marshal({foo: null})
TypeError: Cannot convert undefined or null to object
    at transform (/Users/yusef/Work/Code/aleph/node_modules/ipld/lib/cbor.js:24:23)
    at /Users/yusef/Work/Code/aleph/node_modules/ipld/lib/cbor.js:29:20
    at Array.forEach (native)
    at transform (/Users/yusef/Work/Code/aleph/node_modules/ipld/lib/cbor.js:27:10)
    at Object.exports.marshal (/Users/yusef/Work/Code/aleph/node_modules/ipld/lib/cbor.js:55:22)
    at repl:1:-56
    at ContextifyScript.Script.runInThisContext (vm.js:25:33)
    at Object.exports.runInThisContext (vm.js:77:17)
    at _eval (/Users/yusef/Work/Code/aleph/node_modules/babel-cli/lib/_babel-node.js:103:23)
    at REPLServer.replEval (/Users/yusef/Work/Code/aleph/node_modules/babel-cli/lib/_babel-node.js:185:14)

This can be fixed by adding a null check to the transform function in cbor.js

  function transform (obj) {
    if (obj === null || obj === undefined) return obj
   // ...
  }

This is apparently because javascript thinks typeof null === 'object' 😝

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant