0

i am not getting proper the return after insertgraph in objection.js

i am getting the result like :

[
  User {
    name: 'Santosh Devi',
    city: 'Suratgarh',
    number: '9898987458',
    userroles: UserRoles { role_id: 2, user_id: 37 },
    id: 37
  }
]

where i want the result like :

[
  {
    name: 'Santosh Devi',
    city: 'Suratgarh',
    number: '9898987458',
    userroles: { role_id: 2, user_id: 37 },
    id: 37
  }
]
Kapil
  • 61
  • 1
  • 8
  • 1
    You can do `JSON.parse(JSON.stringify(result))` to convert the objects to plain objects, although a [deep clone function](https://stackoverflow.com/a/40294058/5459839) would also do the job without needing a temporary conversion to string. – trincot Dec 21 '19 at 11:54
  • But one may ask: why is this of any concern to you? – trincot Dec 21 '19 at 11:56
  • can i know what is the difference between both of them @trincot – Kapil Dec 21 '19 at 12:58
  • I added an answer with some explanation. – trincot Dec 21 '19 at 13:27

1 Answers1

1

There are few ways to get rid of the specific class references:

1. JSON.parse(JSON.stringify(result))

This will rebuild the object by first converting the whole object to a string (in JSON format), and then by doing the reverse -- creating a new object from a string. As this string format (JSON) does not store custom class information, it achieves your purpose. However, if your object has functions, symbols, then these will be omitted. Also Map and Set will become empty objects. For a more complete list of restrictions. See JSON.stringify

2. Deep Clone

There are several deep-clone functions out there, that may or may not do what you expect. Some will still try to maintain the original prototype references, so that it would not benefit you. You can find some here: How to Deep clone in javascript. For your case, this one would do the job:

function deepClone(obj, hash = new WeakMap()) {
    if (Object(obj) !== obj) return obj; // primitives
    if (hash.has(obj)) return hash.get(obj); // cyclic reference
    const result = Array.isArray(obj) ? [] : {};
    hash.set(obj, result);
    return Object.assign(result, ...Object.keys(obj).map(
        key => ({ [key]: deepClone(obj[key], hash) }) ));
}

You call it as newResult = deepClone(result).

The advantage here, is that it supports cyclic references, which JSON.stringify cannot handle. Also, there is no string conversion happening, which really is not necessary. You can extend this function to keep deal with some class instances that you like to stay that way. See how you can support Date, RegExp, Map, Set, ... in this answer. But don't do the "catch-all" line.

3. Change the prototype

With this strategy you mutate the result in-place.

function removeClasses(obj, hash = new WeakSet()) {
    if (Object(obj) !== obj) return; // primitives
    if (hash.has(obj)) return; // cyclic reference
    hash.add(obj);
    if (Array.isArray(obj)) Object.setPrototypeOf(obj, Array.prototype);
    else Object.setPrototypeOf(obj, Object.prototype);
    for (let value of Object.values(obj)) {
        removeClasses(value, hash);
    }
}

Call it as removeClasses(result), and afterwards result will have been "fixed". Again, this method does not use a conversion to string. As it does not create a new object either, it consumes less memory. But on the other hand you mutate an object, and some would advise against that.

trincot
  • 317,000
  • 35
  • 244
  • 286