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

nested objects #184

Open
entip opened this issue Jan 16, 2021 · 0 comments
Open

nested objects #184

entip opened this issue Jan 16, 2021 · 0 comments

Comments

@entip
Copy link

entip commented Jan 16, 2021

For anyone who is having problem storing nested object in metadata:
Winston is formatting the object, and adding some extrafield, if you want to store the object as it is
In the winston-mongodb node module, there's a helpers.js file, which contain this function
function cloneMeta(node, opt_parents) { if (!(node instanceof Object) || (node instanceof ObjectID) || (node instanceof Buffer)) { return node; } let copy = Array.isArray(node) ? [] : {}; if (node instanceof Date) { return new Date(node.getTime()); } else if (node instanceof Error) { // This is needed because Error's message, name and stack isn't accessible when cycling through properties copy = {message: node.message, name: node.name, stack: node.stack}; } opt_parents = opt_parents || []; opt_parents.push(node); for (let key in node) { if (!Object.prototype.hasOwnProperty.call(node, key)) { continue; } let value = node[key]; let newKey = key; if (newKey.includes('.') || newKey.includes('$')) { newKey = newKey.replace(/\./g, '[dot]').replace(/\$/g, '[$]'); } if (value instanceof Object) { if (opt_parents.indexOf(value) === -1) { copy[newKey] = cloneMeta(value, opt_parents); } else { copy[newKey] = '[Circular]'; } } else { copy[newKey] = value; } } opt_parents.pop(); return copy; }

Which format meta if type is object, you only have to remove the "not operator" and return the object as it is in return node

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

1 participant