错误 "Unexpected token P in JSON at position 0 in fetch"
Error "Unexpected token P in JSON at position 0 in fetch"
我创建了一个注册表单,在此我将数据从 React 客户端发布到节点服务器,我在节点服务器上创建了一个注册路由来处理该注册表单。
这里我们向服务器发送数据。
async function submitHandler(e){
e.preventDefault();
try{
const response = await fetch("/signup", {
method: "POST",
body: JSON.stringify({
name: name,
email: email,
password: password
}),
headers: {
"Content-Type": "application/json"
}
})
console.log(response);
const data = await response.json();
if(data.error){ //This code of line to make sure that if there is any error then it should be catchable by the catch block.
throw new Error(data.error);
}
}
catch(e){
console.log(e.message); //Here I'm getting error -> unexpected token p in JSON at position 0.
}
}
这是我发布数据的路由。
router.post("/signup", async(req, res) => {
const {name, email, password} = req.body;
try{
const XYZ = await User.ValidatingUser(name, email, password); //Here we are calling a function to check email exist or not before create an account.
const user = new User({
name: name,
email: email,
password: email
})
await user.save();
return res.send("Posted successfully");
}
catch(e){
return res.json({error: e.message});
}
})
这是我在注册路径中调用的函数,用于检查电子邮件是否存在。
//This code is to check that email exists or not. If the email exists then you can't signup.
userSchema.statics.ValidatingUser = async(name, email, password) => {
if(!name || !email || !password){
throw new Error ("Please add all the fields");
}
const user = await User.findOne({email: email});
if(user){
throw new Error("That email is already exist");
}
}
当我点击注册提交按钮时,首先显示错误 -> unexpected token P in JSON at position 0.
但是当我再次点击注册提交表单时,它将显示此错误 -> 该电子邮件已存在(由我们的服务器返回)。
所以这意味着数据正在保存在我们的数据库中。
查看此图片。
enter image description here
我找到了。
这很困难,但这是 res.send("Posted successfully");
的问题。
您要发送一个字符串 JSON,这不是 JSON。
所以,基本上你必须这样做:
res.send({message: "Posted successfully"});
只是:尽量不要 duplicate。
我创建了一个注册表单,在此我将数据从 React 客户端发布到节点服务器,我在节点服务器上创建了一个注册路由来处理该注册表单。
这里我们向服务器发送数据。
async function submitHandler(e){
e.preventDefault();
try{
const response = await fetch("/signup", {
method: "POST",
body: JSON.stringify({
name: name,
email: email,
password: password
}),
headers: {
"Content-Type": "application/json"
}
})
console.log(response);
const data = await response.json();
if(data.error){ //This code of line to make sure that if there is any error then it should be catchable by the catch block.
throw new Error(data.error);
}
}
catch(e){
console.log(e.message); //Here I'm getting error -> unexpected token p in JSON at position 0.
}
}
这是我发布数据的路由。
router.post("/signup", async(req, res) => {
const {name, email, password} = req.body;
try{
const XYZ = await User.ValidatingUser(name, email, password); //Here we are calling a function to check email exist or not before create an account.
const user = new User({
name: name,
email: email,
password: email
})
await user.save();
return res.send("Posted successfully");
}
catch(e){
return res.json({error: e.message});
}
})
这是我在注册路径中调用的函数,用于检查电子邮件是否存在。
//This code is to check that email exists or not. If the email exists then you can't signup.
userSchema.statics.ValidatingUser = async(name, email, password) => {
if(!name || !email || !password){
throw new Error ("Please add all the fields");
}
const user = await User.findOne({email: email});
if(user){
throw new Error("That email is already exist");
}
}
当我点击注册提交按钮时,首先显示错误 -> unexpected token P in JSON at position 0. 但是当我再次点击注册提交表单时,它将显示此错误 -> 该电子邮件已存在(由我们的服务器返回)。 所以这意味着数据正在保存在我们的数据库中。 查看此图片。 enter image description here
我找到了。
这很困难,但这是 res.send("Posted successfully");
的问题。
您要发送一个字符串 JSON,这不是 JSON。
所以,基本上你必须这样做:
res.send({message: "Posted successfully"});
只是:尽量不要 duplicate。