最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - RealmJS - Displays proxy object instead of list of objects - Stack Overflow

programmeradmin3浏览0评论

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.

Am I missing something here?

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.

Am I missing something here?

Share Improve this question asked Feb 12, 2017 at 14:08 Jaseem AbbasJaseem Abbas 5,2306 gold badges54 silver badges74 bronze badges 1
  • Can u expand that proxy and update the question. Want to see what it contains? – Kaushik Commented Feb 12, 2017 at 17:54
Add a comment  | 

4 Answers 4

Reset to default 21

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.

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

The above image is similar to what you showed above.

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.

filtered returns a Results object which is very similar to a javascript Array.

So your code should be: (if you want to display it in the consol)

var items = realm.objects('Item').filtered('id == $0', item_id);
var item = items[0];
console.log(item.name); // should print the name

My suggestion, just loop

   for (let i = 0; i < items.length; i++) {
       console.log('items[' + i + ']:', { ...items[i] })
    }
发布评论

评论列表(0)

  1. 暂无评论