Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
66 views
in Technique[技术] by (71.8m points)

javascript - 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" 
    }); 
  } 
}
question from:https://stackoverflow.com/questions/65879736/get-post-request-node-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...