本文介绍了JSON 中的意外标记 p 在 fetch 中的位置 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个注册表单,在这里我将数据从反应客户端发布到节点服务器,在那里我创建了一个注册路由来处理该注册表单.

I created a signup form, In this I m posting data from react client to the node server where I created a signup route to handle that signup form.

这里我们将数据发布到服务器.

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

当我点击注册提交按钮时,首先显示错误 ->JSON 中位置 0 的意外标记 P.但是当我再次点击注册提交表单时,它会显示这个错误 ->该电子邮件已经存在(由我们的服务器返回).所以这意味着数据正在保存在我们的数据库中.查看此图片.在此处输入图片说明

When I click on the signup submit button, first it shows error -> unexpected token P in JSON at position 0.But When I again clicked on the signup submit the form then it will show this error -> That email already exists (which is returned by our server).So it means data is saving in our DB.See this Image.enter image description here

推荐答案

您没有很好地格式化 JSON.

You didn't well format the JSON.

您在向服务器发送数据时使用了这个 JSON stringify 方法:

You have use this JSON stringify method when you send data to server :

JSON.stringify({
    name: name,
    email: email,
    password: password
})

更新:

真正的问题来自 return res.send("Posted successfully");,但它是格式错误的 JSON.

The real issue come from return res.send("Posted successfully");, but it's well malformatted JSON.

所以,修复方法是一样的:return res.send({error: "Posted successfully"});

So, the way to fix it is the same: return res.send({error: "Posted successfully"});

这篇关于JSON 中的意外标记 p 在 fetch 中的位置 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:22