我有一个表单,并且由于文件上传,我无法使用onClick +函数来处理提交,因此需要将操作指定为表单的属性。但是在采取行动之后,我想处理回应。如何获得我想要的? 代码:
app.post('/register', (req, res) => {
const {email, name, password, passwordConfirm} = req.body;
let image;
if(password === passwordConfirm){
const hash = bcrypt.hashSync(password);
if(req.files){
image = req.files.image;
image.mv('../public/images/' + image.name, err => {
if(err){console.log(err)}
});
}
db('users')
.insert({
email,
hash,
username: name,
profimg: image.name || ""
})
.returning('*')
.then(user => {
res.json(user[0]);
})
.catch(err => res.status(400).json('unable to register'))
} else {
res.status(400).json("Passwords aren't matching")
}
})
<form ref='uploadForm'
id='uploadForm'
action='http://localhost:3001/register'
method='post'
encType="multipart/form-data">
<div className="inputs">
<input
onChange={this.onNameChange}
type="text"
name="username"
placeholder="Username"/>
<input
onChange={this.onEmailChange}
type="email"
name="email"
placeholder="E-mail"/>
<input
onChange = {this.onPasswordChange}
type="password"
name="password"
placeholder="Password"/>
<input
onChange = {this.onPasswordConfirmationChange}
type="password"
name="passwordConfirm"
placeholder="Confirm Password"/>
<input type="file" name="image" />
</div>
<button type="submit">
Register
</button>
</form>
它是React和Node。