Giter Club home page Giter Club logo

Comments (7)

antony avatar antony commented on July 28, 2024

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

from flat.

vcxiaohan avatar vcxiaohan commented on July 28, 2024

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.

from flat.

antony avatar antony commented on July 28, 2024

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.

from flat.

juanjoDiaz avatar juanjoDiaz commented on July 28, 2024

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.

from flat.

antony avatar antony commented on July 28, 2024

@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

from flat.

juanjoDiaz avatar juanjoDiaz commented on July 28, 2024

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 :)

from flat.

antony avatar antony commented on July 28, 2024

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.

from flat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.