'Object passed by server cannot see the function in a class

I am passing an object from server and I cannot see it pass functions.

here is my code.

const express = require('express');
const app = express();
const path = require('path');
//const router = express.Router();
const port = process.env.PORT || 8080;
//const __dirname = path.resolve();
app.use(express.static(path.join(__dirname)));
app.set('views', __dirname + '/');
app.engine('html', require('ejs').renderFile);
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const Obj = require('./classfile')

app.get('/', function(req, res){
  res.render('main.html');
});

app.post('/', async function(req, res) {
  

  this.newObj = new Obj("Hyundai","2022");
  console.log(this.newObj);
  res.status(200).json(this.newObj);


});
app.listen(port);
console.log('Server started at http://localhost:' + port);

classfile.js file.

class Car {
    constructor (name, year) {
      this.name = name;
      this.year = year;
      //this.Func() = Func(tex);
      
    }
    Func(tex){
      return tex+" class";
    }
  }

module.exports = Car;

mainfile.js

function fetcher() {
    fetch('/',
      {
        method: "POST",
        body: JSON.stringify({"name": document.getElementById("name").value}),
        headers: { "Content-Type": "application/json" }
        }
    )

    .then(function(response) {
      return response.json();
    })
    .then(function(VIN) {
      var msg = eval(VIN);
      console.log(msg);
      
    });
  }

Here is the object before I pass it from express server

Car {name: 'Hyundai', year: '2022'} name:'Subaru' year:'2022' [[Prototype]]:Object constructor:class Car {\n constructor (name, year) {\n this.name = name;\n this.year = year;\n
//this.Func() = Func(tex);\n \n }\n Func(tex){\n
return tex+" class";\n }\n } Func:ƒ Func(tex){\n return tex+" class";\n } [[Prototype]]:Object

Here is what I see in browser console.

{name: 'Hyundai', year: '2022'}
name: "Subaru"
year: "2022"
[[Prototype]]: Object
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
__proto__: (...)
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()


Solution 1:[1]

Functions are a special type. You cannot treat them as vanilla objects and pass them through network. In javascript classes are translated into functions, so if you wanna pass the object - just use plain object instead of a class and use prototypical inheritance like so:

let Car = {
  name: undefined,
  year: undefined,
}

// inside server(express) file

let myObj = Object.create(
  Car,
  {
    name: {
      value: "Hyundai",
    },
    year: {
      value: 2022,
    },
  }
);

console.log(myObj); // { name: "Hyundai", year: 2022 }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Gabrielyan