question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
GitMurfcommented, 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!

0reactions
agurtovoycommented, 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 = { ... }).

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB Result can console.log() but not pushed to array ...
This can be solved using async-await, write async to the function of then block-like, mongoclient.connect().then( async (cli) => { db ...
Read more >
12 Ways to Improve Your DevTools Console Logging
Open your browser DevTools with F12 , Ctrl | Cmd + Shift + I , or cmd + option + j and jump...
Read more >
Node.js MongoDB Find
Find One. To select data from a collection in MongoDB, we can use the findOne() method. The findOne() method returns the first occurrence...
Read more >
Top 10 Most Common Node.js Developer Mistakes
There are a number of reasons why a developer may feel tempted to use this to debug his code; however, it is strongly...
Read more >
Console | Node.js v19.3.0 Documentation
A Console class with methods such as console.log() , console.error() , and console.warn() that can be used to write to any Node.js stream....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found