Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly import/export a method in node js

I am currently trying to import a method from a class to keep everything tidy since the method I plan to import will be quite long. However, I seem to get a new error every time I switch it up. The biggest issue is the import/export and the "event" parameter that don’t seem to want to work with me.

Edit: The code works if I simply combine both classes and put the method in ImportingClass.js, however it would be too much code.

ValidateInput.js:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class ValidateInput
{
    validateInput(event)
    {
        var ID = event.target.StudentID.value;
        // ...
    }
}

ImportingClass.js:

import {validateInput} from '../ValidateInput';

class ImportingClass
{

    handleSubmit(event)
    {
        validateInput(this.event);
        // ...
    }
}

Error: Uncaught TypeError: (.validateInput) is not a function

>Solution :

But we can do something like this as well

create an independent function validateInput

export function validateInput(e){
 .... code
}

and in order to call this function in your first class that is ValidateInput.js
we can do something like this

class ValidateInput{
  validateInput(){
    validateInput.apply(this, ...args)
}}

and in your second file ImportingClass.js you can import and use it
with import {validateInput} from './validateInput'

or you can export it via Prototype something like this

export const validateInput = ValidateInput.prototype.validateInput
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading