FRANK GO

What are they?

JSON String:

{

“firstName” : “name”,

….

}

How to convert between them?

class Human {

constructor() {

this.height = 180;

this.weight = 180;

}

getWeight() { return this.weight; }

}

const me =new Human()

  1. Convert to plain object

const {…plainObj} = me

console.dir(plainObj)

2. Helper function

function toObject(proto) {

let jsoned = {};

let toConvert = proto || this;

Object.getOwnPropertyNames(toConvert).forEach((prop) => {

const val = toConvert[prop];

if (typeof val === ‘function’) {

jsoned[prop] = val.bind(jsoned);

return;

}

jsoned[prop] = val;

});

const inherited = Object.getPrototypeOf(toConvert);

if (inherited !== null) {

Object.keys(this.toObject(inherited)).forEach(key => {

if (typeof inherited[key] === ‘function’) {

jsoned[key] = inherited[key].bind(jsoned);

return;

}

jsoned[key] = inherited[key];

});

}

return jsoned;

}

console.dir(toObject(me))

Please note that .create takes an array!!

// Insert one new `Character` document
await Character.create({ name: 'Jean-Luc Picard' });

// Insert multiple new `Character` documents
await Character.create([{ name: 'Will Riker' }, { name: 'Geordi LaForge' }]);

// Create a new character within a transaction. Note that you **must**
// pass an array as the first parameter to `create()` if you want to
// specify options.
await Character.create([{ name: 'Jean-Luc Picard' }], { session });

I