0

I have input for username and password, when user click submit, they are sent with POST method, and when node.js receives them, they will be put in constructor class, something like this:

app.post('submit1', (req, res) => {
    var data = req.body;
    new User(data);
    res.send(i want to send response if that bellow is true)
})

After that, in class User, there is a function loginClient(data), and inside that function is function for verification waiting for some emit, and it that functions receives that emit, username and password are correct, and I want to replay to that 'submit1'.. How can I do this?

class User { 
  loginClient(data){ 
    this.somthing.on('thatEmit', () => { 
      "send response because this function got that emit" 
    }); 
  } 
}

2 Answers 2

1

In your case, you'd have to add a callback inside the function located inside the user constructor.

app.post('submit1', (req, res) => {
    var data = req.body;
    var user = new User(data);
    user.loginClient(data, function(valid){
    if(valid){
      res.send(i want to send response if that bellow is true)
    }else{
     res.send('false')
   }
    })   
})

Then inside your User class your function for instance would be loginClient

function loginClient(callback){
//do whatever you need to do to validate
  this.somthing.on('thatEmit', () => { 
      //"send response because this function got that emit" 
     if(callback){
      callback(true) //this will call function that's in the post
      }
   }); 
 
}
Sign up to request clarification or add additional context in comments.

5 Comments

How can I call a function that is inside "app.post().." in one of User class functions? I can't call it because of the scope
in User class i have loginClient(data) function, and inside that function is function that is waiting for emit
Can you show the function? So that I cannot make any more assumptions?
loginClient(data, callback){ this.somthing.on('thatEmit', () => { // "send response because this function got that emit" if(callback) callback(); }); }
Updated my post to reflect the changes
0

A response from the server is compulsory, no matter it is correct or not. Your verification function that receives arguments can return a value like this:

var x = funct(argument)

function funct(argument) {
// do something
return something
}

However, you can send messages like true or false in your response.

Like:

res.send('true')

or

res.send('false')

1 Comment

That isn't my question. I asked, how I can send a response inside that function which is inside the User constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.