Comments (7)
Had the same problem, so I fixed it in #61 - let me know if it works for you
from flat.
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.
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:
- .toString does not unwrap ObjectIDs. valueOf does. .toString() on its own will print out the object.
- Your code assumes that every _id field is an ObjectID. We can't make sweeping assumptions like this
- Your code assumes that only _id fields are ObjectIDs. This also isn't true (reference field types are ObjectID, for example)
- 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.
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.
@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.
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.
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)
- What option to make difference between object & array HOT 1
- Possible to keep parent key HOT 3
- High severity vulnerability detected HOT 3
- Why cats are also flattened? 🐈 HOT 1
- Deno support HOT 2
- maxDepth unflatten
- High security vulnerability in 4.1.1 HOT 1
- Unflatten method doesn't work with "toString" key
- Unflatten partial arrays
- Need a maintainer? HOT 6
- Rename `master` branch to `main`
- [question] how could we define don't flat some specific object by the key?
- Release version 6 HOT 25
- Feature request: add ability to prevent modifying certain value in `unflatten` HOT 2
- Support for cyclic objects
- Support for typescript HOT 3
- Jest encountered an unexpected token HOT 6
- ERR_REQUIRE_ESM Add commonjs support HOT 3
- Can't import in jest w v6.0 HOT 1
- Empty objects are ignored by flatten
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flat.