gpt4 book ai didi

javascript - 引用错误: email is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:32 25 4
gpt4 key购买 nike

嗨,请帮助陷入困境的开发人员。

我一整天都在尝试解决这个问题,但没有成功。基本上我想做的就是从我的 AddUsers 类发布并将其存储到我的 sql 数据库中。这是一个非常简单的查询,但已经让我变得更好了!状态会在更改时更新,但似乎是 server.js 的问题(错误包含在帖子底部)

服务器.js


app.post("/admin-Add-Users", function(req, res) {
var request = new sql.Request();

// query to the database and get the records
request.query(
"insert into Login (email, password) values ('" +
req.body.email +
"','" +
req.body.password +
"')",
function(err, recordset) {
if (err) console.log(err);
}
);
res.send({ message: "Success" });
});

添加用户类

class AddUsers extends React.Component {
constructor() {
super();

this.state = { users: [], email: "", password: "" };
this.onSubmit = this.handleSubmit.bind(this);
}

handleSubmit(e) {
e.preventDefault();
const data = { email: this.state.email, password: this.state.password };

fetch("/admin-Add-Users", {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log("Success:", data);
})
.catch(error => {
console.error("Error:", error);
});
}

render() {
console.log(this.state.users);
return (
<div>
<LoginForm></LoginForm>

<form>
<input
type="text"
placeholder="email"
value={this.state.email}
onChange={e => this.setState({ email: e.target.value })}
/>
<input
type="text"
placeholder="password"
value={this.state.password}
onChange={e => this.setState({ password: e.target.value })}
/>
<input type="submit" onClick={this.onSubmit} />
</form>
</div>
);
}
}
ReferenceError: email is not defined

更新:在尝试了给我的建议后,我现在又出现了一个新错误。

Error: SyntaxError: Unexpected token < in JSON at position 0 

最佳答案

您的 React 应用程序似乎没有任何问题。

The problem is at your API end where you're formulating an insert query without actually reading the request json content (email & password) fields.

您可以在生成查询之前添加以下行。

// create sql obj
...
var email = req.body.email;
var password = req.body.password;

...
// your query

关于javascript - 引用错误: email is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com