Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:提取失败,提取后状态=已取消_Javascript_Node.js_Reactjs_Api - Fatal编程技术网

Javascript TypeError:提取失败,提取后状态=已取消

Javascript TypeError:提取失败,提取后状态=已取消,javascript,node.js,reactjs,api,Javascript,Node.js,Reactjs,Api,我使用后期获取api将用户输入数据从React js传递到节点js,即后端,并成功地将数据存储到我的数据库中。但是获取api没有成功返回对象,并且在google devtool中向我显示与已取消的相同的网络状态。 我什么都试过了,但不知道怎么解决。谢谢 错误屏幕截图 CustomerRegistration.jsx const onSubmit = async (e) => { const { fname, lname, email, password, address

我使用后期获取api将用户输入数据从React js传递到节点js,即后端,并成功地将数据存储到我的数据库中。但是获取api没有成功返回对象,并且在google devtool中向我显示与已取消的相同的网络状态。 我什么都试过了,但不知道怎么解决。谢谢

错误屏幕截图

CustomerRegistration.jsx

const onSubmit = async (e) => {

        const { fname, lname, email, password, address } = state;

        await fetch('/customer-registration', {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                fname: fname, lname: lname, email: email, password: password, address: address
            })
        }).then((res)=>{
            console.log(`this is my res ${res}`);
            window.alert('Customer Registration successfull');
        }).catch((error)=>{
            window.alert(error);
            console.log(error);
        })
    }
Routers.js

router.post('/customer-registration',async(req,res)=>{
    
    const {fname,lname,email,password,address}=req.body;
    
    try {
        const valid=await myModel.findOne({email:email});
        if(valid){
            const flag=true;
           console.log('Email already exist');
        }else{

            const finalData=new myModel({fname,lname,email,password,address});
            const data=await finalData.save();
            if(data){
                console.log('Data saved to db');
                console.log(data);
                res.json(data);
            } 
        }
    } catch (error) {
        console.log('Data not saved');
    }

})

您得到该错误是因为您没有确保您的NodeJ返回响应

router.post('/customer-registration',async(req,res)=>{
    
    const {fname,lname,email,password,address}=req.body;
    
    try {
        const valid=await myModel.findOne({email:email});
        if(valid){
            const flag=true;
           console.log('Email already exist');
           
        }else{

            const finalData=new myModel({fname,lname,email,password,address});
            const data=await finalData.save();
            if(data){
                console.log('Data saved to db');
                console.log(data);
                return res.json(data);
            } 

           return res.json({ok: false }) //you need to ensure you return something.
        }
    } catch (error) {
        console.log('Data not saved');
        return res.status(400).end() // you need to return something even if it's an error.
    }

})

您没有取消表单提交。。。。您可以清楚地看到表单提交发生在网络调用中取消请求后的行中。但仍然会收到相同的错误。您是从按钮调用Submit,还是从
调用?如果您正在使用,请在onSubmit函数中包含
e.preventDefault
?谢谢兄弟,这很好。在Routers.js代码中可以看到一个变量名标志,此时提出了一个问题,我想在CustomerRegistration.jsx中访问它的值,我如何才能做到这一点。谢谢。您可以将其作为回复返回
res.json({flag:true})