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

Improve the output from console.log of objects and collections on Node.js #2758

Closed
kraenhansen opened this issue Mar 18, 2020 · 9 comments · Fixed by #6315
Closed

Improve the output from console.log of objects and collections on Node.js #2758

kraenhansen opened this issue Mar 18, 2020 · 9 comments · Fixed by #6315

Comments

@kraenhansen
Copy link
Member

kraenhansen commented Mar 18, 2020

Goals

Using console.log during debugging is a fairly common practice and the output of calling this on an object or collection from a Realm should be informative.

Expected Results

I would expect that logging a Collection of objects (such as Results) would give information on the type of items in the collection, the size of the collection and perhaps a preview of the first couple of elements in the collection. As a reference, calling console.log on an array and objects yields respectively:

console.log(a);
// [ { name: 'Alice' }, { name: 'Bob' }, { name: 'Charlie' } ]
console.log(a[0])
// { name: 'Alice' }

Actual Results, Steps to Reproduce & Code Sample

const Realm = require('realm');
const realm = new Realm({ schema: [{ name: "Person", properties: { name: "string" } }] });
realm.write(() =>{ realm.create('Person', { name: "Alice" }); });
const persons = realm.objects("Person");
console.log(persons)
// Results {}
console.log(persons[0])
// Person { [Symbol(_external)]: [External] }

In addition to this, in the Node.js REPL, when executing an expression the REPL prints its value, for a collection, this prints:

Proxy [ Results {}, {} ]

All of the output above have very limited value to a user while debugging their code.

NOTE: https://nodejs.org/dist/latest-v12.x/docs/api/util.html#util_custom_inspection_functions_on_objects might come in handy when solving this.

Version of Realm and Tooling

  • Realm JS SDK Version: 5.0.0
  • Node or React Native: Node.js
  • Client OS & Version: Tested on MacOS
  • Which debugger for React Native: None
@blagoev
Copy link
Contributor

blagoev commented Mar 18, 2020

I am not aware of any way to do this without overriding the global console.log function. and the util.inspect is not cross platform.
imo, the general accepted way is doing console.log(JSON.stringify(obj)) which works in v5
We might as well extend this to collections to print the first three items for example.

@kraenhansen
Copy link
Member Author

kraenhansen commented Mar 18, 2020

I am not aware of any way to do this without overriding the global console.log function.

We need to define a function and store it on the Realm.Object prototype as the [util.inspect.custom] symbol: See https://nodejs.org/dist/latest-v12.x/docs/api/util.html#util_custom_inspection_functions_on_objects

imo, the general accepted way is doing console.log(JSON.stringify(obj))

I disagree: I see no reason we would ask that of our users and I would not expect an average user to think of that when being frustrated while debugging their app. I believe we can (and should) do better. Also - this wouldn't work for a large collection or objects with cyclic relationships.

I don't think we need to strive for a cross platform solution here, to my knowledge the console API is not standardized - and if other platforms have similar issues, we should of course capture issues and try to fix that too.

@blagoev
Copy link
Contributor

blagoev commented Mar 19, 2020

Let me paraphrase my comment. There is no way i can see we can support console.log the way you have described without overriding the global.console.log function. The util.inspect.custom is a nodejs way to provide support for custom output for util.inspect only. Which is a step away from the usability you want to achieve using console.log. since its behind a 'require' and then the code doing it is not crossplatform

@joaodematejr
Copy link

joaodematejr commented Mar 20, 2020

"react-native": "0.61.5",
"realm": "^5.0.1",
+1

@joaodematejr
Copy link

Captura de Tela 2020-03-20 às 11 20 44

@kraenhansen
Copy link
Member Author

kraenhansen commented Mar 20, 2020

The util.inspect.custom is a nodejs way to provide support for custom output for util.inspect only. Which is a step away from the usability you want to achieve using console.log.

I'm mentioning this because console.log calls util.inspect on all arguments passed to it.
This is output from the Node.js REPL:

> const util = require("util")
undefined
> const a = { [util.inspect.custom]: () => "Hello" }
undefined
> a
Hello
> console.log(a)
Hello
undefined

since its behind a 'require' and then the code doing it is not crossplatform.

I don't think I understand that comment. We can easily attempt a require in a way that wouldn't interfere with other platforms?

@blagoev
Copy link
Contributor

blagoev commented Mar 21, 2020

I am not against the initial intent of this issue. Anything that can ease the developer is a must have. But I think we need to find a good cross platform way of doing object inspection which provides the same behavior on all platforms, whenever possible.

const a = { [util.inspect.custom]: () => "Hello" }
console.log(a)
Hello

Unfortunately this does work under the VS Code debugger. Which goes to show even on nodejs environment this seems not reliable/ideal way of doing it. Having this working, depending if a debugger is attached or not will be even more confusing.

A better option would be providing this ourselves in the form of Realm.inspect(obj) which outputs to the console and will work both for RN and nodejs and can be done to work with any arbitrary object and array it is passed so it can be reused across the project and can be much easily switched off (redefined) by the developer when releasing the project and so on...

@agurtovoy
Copy link

agurtovoy commented Apr 12, 2021

For what it's worth, this used to work. I'm upgrading from realm 2.17.0 to 10.3.0, and I have a bunch of failing Jest snapshots that look like this:

Screen Shot 2021-04-11 at 8 45 30 PM

(Jest uses pretty-format for serializing objects into string).

EDIT: Looks like in case of Jest, this issue specifically affects Realm object types that are defined using JavaScript classes (class Car { static schema = {...} } vs. const Car = { ... }).

@GitMurf
Copy link

GitMurf commented May 4, 2022

A better option would be providing this ourselves in the form of Realm.inspect(obj) which outputs to the console and will work both for RN and nodejs and can be done to work with any arbitrary object and array it is passed so it can be reused across the project and can be much easily switched off (redefined) by the developer when releasing the project and so on...

Curious if any updates here? Is there an alternative option to console.log() that can show more about the Realm objects? Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants