From 4c39b0711da6a1ff9e3802f7bac8f218687fb239 Mon Sep 17 00:00:00 2001 From: Steffen Agger Date: Fri, 10 Jul 2020 16:10:07 +0200 Subject: [PATCH] toJSON checks for Realm.Object or Realm.Collection before calling toJSON recursively. --- lib/extensions.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/extensions.js b/lib/extensions.js index ec657673ef..a9d3252518 100644 --- a/lib/extensions.js +++ b/lib/extensions.js @@ -82,7 +82,8 @@ module.exports = function(realmConstructor, context) { Object.defineProperty(realmConstructor.Collection.prototype, "toJSON", { value: function (_, cache = new Map()) { - return this.map((item, index) => item.toJSON ? item.toJSON(index.toString(), cache) : item); + return this.map((item, index) => + item instanceof realmConstructor.Object ? item.toJSON(index.toString(), cache) : item); }, writable: true, @@ -112,8 +113,10 @@ module.exports = function(realmConstructor, context) { Object.keys(this) .concat(Object.keys(Object.getPrototypeOf(this))) .forEach(key => { - // TODO: check that we're dealing with a Realm type, before passing in cache? - result[key] = this[key].toJSON ? this[key].toJSON(key, cache) : this[key]; + const value = this[key]; + result[key] = value instanceof realmConstructor.Object || value instanceof realmConstructor.Collection + ? value.toJSON(key, cache) + : value; }); return result;