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 pass parameter from one page function to another page class function in Javascript / Node js

I want to pass parameter from my app.js page to another page User.js which contain class function but all I am getting is undefined value. I am trying to pass selected choice from the inqurier but its not getting passed.

User.js

class User{
  setUser(name){
    let userThing= fname(name);
    ...
    ...
    return userThing
  }

  getUser(){
    ...
    ...
    return
  }
}

module.exports.User=User;

app.js

const { Command } = require("@oclif/command");
const inquirer = require("inquirer");
const config = require("./User.js");

class RegionCommand extends Command {
 async run(){
   inquirer
   .prompt({
      type: "list",
      name: "test",
      message: " Select Nickname: ",
      default: false,
      choices: ["Tom", "Jerry"],
      })
      .then((answers) => {
        var selectedName=answers.test;
        // var nameDetails= this.config.User.setUser(selectedName); I tried this method too but still not working.
        var nameDetails= config.User.setUser(selectedName);
      });
 }
}

how can I pass my inqurier value app.js to user.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

>Solution :

You are trying to call a normal function like a static function. You first need to make a class instance!

const { Command } = require("@oclif/command");
const inquirer = require("inquirer");
const { User } = require("./User.js");

class RegionCommand extends Command {
 async run(){
   inquirer
   .prompt({
      type: "list",
      name: "test",
      message: " Select Nickname: ",
      default: false,
      choices: ["Tom", "Jerry"],
      })
      .then((answers) => {
        var selectedName=answers.test;
        const user = new User(); // create an instance of the class "User"
        var nameDetails= user.setUser(selectedName);
      });
 }
}
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