Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectId from mongodb will not correct by using flatten methods #60

Open
vcxiaohan opened this issue Jul 30, 2017 · 7 comments
Open

ObjectId from mongodb will not correct by using flatten methods #60

vcxiaohan opened this issue Jul 30, 2017 · 7 comments

Comments

@vcxiaohan
Copy link

No description provided.

@antony
Copy link

antony commented Aug 1, 2017

Had the same problem, so I fixed it in #61 - let me know if it works for you

@vcxiaohan
Copy link
Author

It not work for me!
The mongodb data like this:

{ 
_id: 598124cbfd5a7c47dcae586f,
  updatedAt: 2017-08-02T01:03:07.988Z,
  createdAt: 2017-08-02T01:03:07.988Z,
  title: '111'
} 

When I use var flat = require('flat'); var _obj = flat.flatten(obj), the result like this:

{
 '_id._bsontype': 'ObjectID',
  '_id.id': <Buffer 59 81 24 cb fd 5a 7c 47 dc ae 58 6f>,
  updatedAt: 2017-08-02T01:03:07.988Z,
  createdAt: 2017-08-02T01:03:07.988Z,
  title: '111'
 }

Suggest: You just need to convert type from ObjectId to String by coding like this:

if(key == '_id' && isBuffer(key)) {
obj[key] = (obj[key]).toString()// this will convert type from ObjectId to String
}

And I will try again after you fixed this.

@antony
Copy link

antony commented Aug 2, 2017

Works fine for me, It's already in production on our application :)

You need to:

npm install vendigo-group/flat # use my branch until this PR is merged.

Then use the following code:

const coercion = [{
  test: (key, value) => value && value._bsontype === 'ObjectID',
  transform: value => { return value.valueOf().toString() }
}]

flatten(yourobject, { coercion: coercion })

There are tests covering the code in the pull request, so feel free to inspect those to help you.

The code you have suggested, by the way, has a number of issues:

  1. .toString does not unwrap ObjectIDs. valueOf does. .toString() on its own will print out the object.
  2. Your code assumes that every _id field is an ObjectID. We can't make sweeping assumptions like this
  3. Your code assumes that only _id fields are ObjectIDs. This also isn't true (reference field types are ObjectID, for example)
  4. For some reason, you are checking isBuffer on the key, not the value. This will return false. The key is _id which is a String.

@juanjoDiaz
Copy link

In #64 I've proposed yet another solution using the toJSON method.
Since flatten is a library to flatten JSON objects, it makes sense to use the toJSON method which is used by browsers/node JSON utility object.

@antony
Copy link

antony commented Sep 11, 2017

@juanjoDiaz your solution is fine - however its scope is very small - there are precious few objects which are generous enough to provide a toJSON method. I'm also not sure it's always the user's preference to use the toJSON method. It's good to provide the user with a choice.

Also - as a side note, you left a console.log in your code

@juanjoDiaz
Copy link

damn! good catch...
Some eslint would have been useful here...

Yeah. Certainly the scope is small. The idea was to modify just the minimum and cover the most possible cases. As I mention in the PR, 'm happy with any solution that cover this use case :)

@antony
Copy link

antony commented Dec 11, 2017

I've decided to fork and republish as flatley, as I get the feeling that this project is abandoned.

https://www.npmjs.com/package/flatley
https://www.github.com/antony/flatley

Let me know if you have further issues (ideally provide me with a reproduction) and I'll endeavour to fix them.

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

No branches or pull requests

3 participants