-
Type:
Task
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Not Needed
-
None
-
None
-
None
-
None
-
None
-
None
https://github.com/mongodb/js-bson/issues/356
Map Object accepts a non-string value for the key, but it looks like the serializer doesn't allow it.
The code below causes an error:
Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
const mapObject = new Map() mapObject.set({foo: 'bar'}, 'foo') mapObject.set(undefined, 'foo') mapObject.set(NaN, 'bar') mapObject.set(3, 'baz') //-> Map (4) {{…} => "foo", undefined => "foo", NaN => "bar", 3 => "baz"} // use MongoDB Node.JS Driver await collection.insertOne({ data: mapObject, }) //-> TypeError: argument must be a string
Functions such as serializeString take a key as an argument, but it is uncertain whether the Map Object's key has a string, thus buffer.write() may raise a TypeError.
It can work around that by converting the key to a string and passing it in the serializeInto, but I don't know if that's a good way, as it easily causes duplicate keys.