Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RealmJS - Displays proxy object instead of list of objects

I am using Realm JS with my React Native app. I had inserted a value in my schema as follows.

const CarSchema = {
  name: 'Car',
  properties: {
    model: 'string'
  }
};

let realm = new Realm({schema: [CarSchema]});
realm.write(() => {
  realm.create('Car', {
    model: 'Ford'
  });
});

let cars = realm.objects('Car');
console.log(cars);

In the Chrome console I get the following object instead of all the cars in the db.

enter image description here

Am I missing something here?

like image 284
Jaseem Abbas Avatar asked Feb 12 '17 14:02

Jaseem Abbas


People also ask

What are proxy objects?

The Proxy object allows you to create an object that can be used in place of the original object, but which may redefine fundamental Object operations like getting, setting, and defining properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on.

What is proxy object in JavaScript?

Proxy is an object in javascript which wraps an object or a function and monitors it via something called target. Irrespective of the wrapped object or function existence. Proxy are similar to meta programming in other languages.

What is Realm in JavaScript?

Realm is a mobile database that runs directly inside phones, tablets or wearables. This project hosts the JavaScript versions of Realm. Currently we support React Native (both iOS & Android), Node.


2 Answers

This is a consequence of how Realm objects work (as well as Lists and Results) - instead of hydrating data from the database and deserializing (potentially) a ton of objects, Realm gives you lazily-loaded objects and collections. Realm.objects('Car') doesn't return a JavaScript array, but a Results object - it still behaves exactly like an array, but it's not implemented using the built-in array object. That's why it doesn't look like an array of objects in the Chrome console, even though it behaves the same.

You can, however, eagerly read a Realm collection in an Array with Array.from(realm.objects('Car')) and that will visualize better in the debugger.

Background

"Proxy" objects are the corner-stone of Realm and its zero-copy philosophy. All the Realm SDKs are implemented the same way - instead of plain objects subject to serialization and deserialization, you get proxy objects that access the database directly.

In realm-js the actual proxying mechanism works differently based on whether your code runs on React Native, Node.js, or the React Native Chrome debugger, so the implementation details of these proxies are going to differ.

The problem with all this is that it doesn't display as nicely in a JavaScript debugger. Unfortunately I'm not aware of a way to influence the way Chrome displays objects in the debugger.

Source: I'm a realm-js contributor.

like image 184
Tayschrenn Avatar answered Nov 03 '22 17:11

Tayschrenn


The code seems fine, I used a similar snippet in my project as well

Unexpanded

The above image is similar to what you showed above.

Expanded object

The exanded version should contain all the objects of Car. In my case the properties has key and value under object 0. In your case it should have model: "Ford" in one the results.

like image 24
Kaushik Avatar answered Nov 03 '22 17:11

Kaushik