Cone
Cone

Reputation: 3

Get/Post request node.js

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" 
    }); 
  } 
}

Upvotes: 0

Views: 117

Answers (2)

Haich
Haich

Reputation: 269

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
      }
   }); 
 
}

Upvotes: 1

Great Sage
Great Sage

Reputation: 46

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')

Upvotes: 0

Related Questions