Extraction of properties from an object in an ES6 class

How should object destructuring be properly applied for methods within ES6 classes?

user.ts

import { Request, Response } from "express";

export class User {

  constructor (){
      Object.assign(this,{
          root:this.root,
          get:this.get
      })
  }  
  public  root(req: Request, res: Response) { 
    res.status(200).send({
      message: "DEFAULT request successful!!"
    });
  }
  public get(req: Request, res: Response){
    res.status(200).send({
        message: "USER request  successful!!"
      });
  }

}

export const user = new User();

And I import it like this

import  {root,get} from './user'

However, it throws a "has no exported member" error

UPDATE

Following comments, I modified my export to the following

let obj = new User();

export const user = {
    root:obj.root,
    get:obj.get
}

The same error persists

Answer №1

In order to properly export the root and get functions, you should destructure your export line like this:

export const { root, get } = new User();

If you also wish to export a user along with the functions:

export const user = new User();
export const { root, get } = user;

Alternatively, you can simply import the user and manually create the functions as needed:

import { user } from "./user";
const { root, get } = user;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Don't pay attention to jquery.change

Consider the code snippet below: $(".someSelector").change(function() { // Do something // Call function // console.log('test') }); An issue arises where this code is triggered twice: once when focus is lo ...

AngularJS - Triggering functions on image load event

I have been on a quest to find the best way to handle the onload event for images in Angular using jqLite. I came across this question, but I am looking for a solution that involves directives. Therefore, the approach below is not satisfactory to me: .c ...

Utilize the value of one variable to determine access to another variable in Javascript

I am working with several boolean variables and I want to create a new variable that keeps track of the most recently changed boolean variable. This way, every time a new boolean variable is modified, I can toggle the previous one. If you have any ideas o ...

The download attribute in HTML5 seems to be malfunctioning when encountering a 301 Moved Permanently

I am attempting to create an automatic download feature for a file from a URL with a 301 Moved Permanently redirection. Here is the current code: <a href="myserverapi/download?fileId=123" download="image.jpg" target="_blank" ...

Tips for reducing the size of your JavaScript buffer

Is it the correct way to convert a buffer to a string, trim it, and then convert back to a buffer in Node.js v6.12.x? var buf = Buffer.alloc(xxxxxx); ..... // buffer receives its value ..... let buffParsed = String.fromCharCode.apply(null, buf); buffPa ...

Executing a Cron Job in a Node.js Environment

I am currently working with ExpressJS. In my file at /routes/index.js, I've set up the following: app.group('/test', test => { const testHandler = new testHandler(); test.get('/test-action', testHandler.testAction.bind(t ...

What is the best way to implement asynchronous guarding for users?

Seeking assistance with implementing async route guard. I have a service that handles user authentication: @Injectable() export class GlobalVarsService { private isAgreeOk = new BehaviorSubject(false); constructor() { }; getAgreeState(): Obser ...

Unexpected error: the process is not recognized

I am currently working with node.js to develop a web application. Upon running the application (either by opening index.html in the browser or executing "npm start" in the terminal), I encounter two errors: Uncaught ReferenceError: process is not defined ...

Chrome Extension: Despite adding it to web_accessible_resources, the resource remains unavailable

I'm currently developing a Chrome Extension with React/Redux and utilizing Webpack. As part of the optimization process, I have started migrating resources to a separate file using the WebPack DLLReference plugin. After generating the dll/dll.vendor. ...

Can you extract debugging details from an ajax preflight inquiry?

I am currently working on a JavaScript project that involves making an AJAX request. However, I am encountering issues with the preflight OPTIONS call for this request failing. To provide some transparency to the user, I would like to display debug infor ...

Restrict User File Uploads in PHP

I have a system set up that enables users to upload files under 200 MB. Once the file is downloaded once, it will be automatically deleted. Additionally, all files are deleted from the server after 24 hours. I am looking for a way to limit the number of up ...

What is the process for accessing a table in indexedDB using Dexie.js immediately after it has been opened?

I am faced with the challenge of needing to verify if a specific table already exists in IndexedDB right after it has been opened. However, I am unsure how to access the DexieDB object inside the 'then' statement. this.db = new Dexie("DBNAME"); ...

Processing POST submissions

Seems like a straightforward task, yet I'm struggling to pinpoint the error in my code. app.js var http = require('http'); var express = require('express'); var app = express(); var bodyParser = require('body-parser'); ...

Comparison between the version of a particular dependency and the version of its dependent dependency

Imagine a scenario where I have dependency X version 1.0 and dependency Y version 1.0 defined in my package.json. Would there be any issues if Y requires X version 2.0 (as indicated in the package-lock.json) but I continue to use X version 1.0 in my code ...

The performance of the Express.js interface is currently lacking

Questions for using Express.js: Code: app.use(A) app.use(B) { const isCurlR = app.use(function (req, res, next) { next() }) isCurlR.get('/getLatest', (req, res) => {}) } { const isNotCurlR = app.use(function (req, re ...

A comprehensive guide on retrieving data from API across several pages

Struggling with pulling data from an API using React and facing the challenge of retrieving more than one page of data. "info": { "count": 493, "pages": 25, "next": "https://rickandmortyapi.com/api/character/?pa ...

Tips for sharing common variables across all routes in Express by rendering them from app.js

Within my Node.js application, there are several variables that need to be rendered on every route. With approximately 4-5 common variables across about 20 routes, I find myself repeating the code for passing these variables in each res.render. I am seeki ...

What is the best way to open a browser window at a quarter of its default size?

Is there a way to open a window at 25% of its default device browser window size? I attempted the code below, which worked. However, it only accepts pixel inputs and not relative % values. This makes it non-scalable across various devices. window.resizeT ...

Is there a way to transform a dynamically created JavaScript table into JSON or XML format and then store it as a file?

A dynamic table of properties is created in JavaScript using a function: // update table PropertyWindow.prototype._update = function (text) { if (text === void 0) { text = "&lt;no properties to display&gt;"; } this._propertyWindow.html(text); ...

Debugging a route in Node.js and Express

Currently facing a debugging issue with my NodeJS and Express framework routes. I successfully set up node-inspector and got it running in a Chrome tab. However, I'm not able to see anything displayed when using the code snippet below in my index.js r ...