【问题标题】:Unexpected token p in JSON at position 0 in fetch提取中位置 0 处 JSON 中的意外令牌 p
【发布时间】:2021-09-21 08:52:46
【问题描述】:

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

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

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

当我点击注册提交按钮时,首先它显示错误 -> 在位置 0 的 JSON 中出现意外的令牌 P。 但是当我再次点击注册提交表单时,它会显示此错误 -> 该电子邮件已存在(由我们的服务器返回)。 所以这意味着数据保存在我们的数据库中。 查看此图片。 enter image description here

【问题讨论】:

    标签: node.js reactjs express async-await try-catch


    【解决方案1】:

    你没有很好地格式化 JSON。

    当你向服务器发送数据时,你已经使用了这个 JSON stringify 方法:

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

    更新:

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

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

    【讨论】:

    • 但是当我使用速记属性将数据发布到服务器时,我编写了这行代码。您可以在我使用 fetch 将数据发布到服务器的第一页中看到这一点。当我将数据发布到服务器时,我在第一页写了这个。正文: JSON.stringify({ 姓名,电子邮件,密码 }),
    • 不,body 只是 JSON.stringify({name});,但 JSON 工作为 key: value,所以你必须准确地知道密钥的名称
    • 所以这意味着你说这里的速记属性不起作用。
    • 我也试过这种方法,但也没有用。它仍然显示相同的错误。
    • 好的,所以我建议您记录整个错误对象,也许它会包含更多数据。另外,我建议您:在您的身体创建之外创建 JSON 对象。将其写入控制台,并在正文中使用。然后,通过 json 验证器检查打印的 json 的有效性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2018-06-10
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多