Improve the output from console.log of objects and collections on Node.js
See original GitHub issueGoals
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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9 (5 by maintainers)
Curious if any updates here? Is there an alternative option to console.log() that can show more about the Realm objects? Thanks!
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:
(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 = { ... }
).