Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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/React的函数路由不同的URL(已解决)_Javascript_Node.js_Reactjs_Routes_Axios - Fatal编程技术网

根据使用Javascript/React的函数路由不同的URL(已解决)

根据使用Javascript/React的函数路由不同的URL(已解决),javascript,node.js,reactjs,routes,axios,Javascript,Node.js,Reactjs,Routes,Axios,我想根据登录名路由不同的URL,axios在以下代码中调用该登录名: import React, {Component} from "react"; import axios from "axios"; import "./ComponentStyles.css"; class Login extends Component { state = { username: "",

我想根据登录名路由不同的URL,axios在以下代码中调用该登录名:

import React, {Component} from "react";
import axios from "axios";
import "./ComponentStyles.css";

class Login extends Component {

    state = {
        username: "",
        password: ""
    }

    handleChange = ({target}) => {
        const { name, value } = target;
        
        this.setState({
          [name]:value
        });
    };

    Login = (event) => {
        event.preventDefault();
        const User = {
            username: this.state.username,
            password: this.state.password
        }
  
        axios({
            url:"/api/login",
            method: "POST",
            data: User
        })
        .then((res) => {
            if (res.data === true) {  // previously, I used only res, and it didn't work out
                console.log('Logged in!');
                window.location = '/Menu';
            }
            else {
                window.location = '/';
                console.log('Failure. Could not Login.');
        })
        .catch((error) => {
            console.log(error);
            window.location = '/';
        })
      };

    // more code

}
在这里,我想根据函数
user\u authMySQL
的结果设置
窗口的位置,该函数位于另一个代码中:

const express = require("express");
const router = express.Router();

// some code

router.post('/login', async (req, res) => {
    const User = req.body;  // Object type: User
    var resultTemp = false;

    // This function recieves a username, password, and callback function
    // result in callback function is true if login succeeds, otherwise it's false
    await user_authMySQL(User.username, User.password, function (result) {
        resultTemp = result;
        console.log(resultTemp); // To see what's stored
        if (resultTemp === false ) 
            console.log("Cannot login, data is invalid.");
        else console.log("Loggin in!");
        res.json(resultTemp);  // To write the result
    });  

    // console.log(resultTemp + ' = resultTemp'); // This didn't work beacuse of the async function
    // res.json(); // This line was put inside the callback function
});
当我运行它并使用无效的登录数据时,终端结果如下:

[Server] false = resultTemp
[Server] POST /api/login 200 - - 5.987 ms
[Server] false
[Server] false
[Server] Cannot login, data is invalid.
当使用正确的登录数据时,结果如下:

[Server] false = resultTemp
[Server] POST /api/login 200 - - 1.258 ms
[Server] true
[Server] true
[Server] Loggin in!
问题是,应用程序始终执行
window.location='/'
,但不执行
console.log()
中的任何条件

        .then((res) => {
            if (res === true) {
                console.log('Logged in!');
                window.location = '/Menu';
            }
            else {
                window.location = '/';
                console.log('Failure. Could not Login.');
        })
        .catch((error) => {
            console.log(error);
            window.location = '/';
        })

我不知道如何根据功能来路由不同的URL,欢迎提供任何帮助:)

您的代码有几个问题,我强烈建议您重新考虑如何登录,否则您需要从服务器端发送“true”值,以便稍后可以对其进行测试
res.json(true)
在您的情况下
之后,您可以通过
res.data==true

来测试它。您的代码有几个问题,我强烈建议您重新考虑如何登录,否则您需要从服务器端发送“true”值,以便稍后在您的案例中测试它
之后,您可以通过
res.data==true

对其进行测试
res
是纯布尔值还是需要访问的响应对象?您是否检查过开发工具中的“网络”选项卡或将其记录下来以查看其实际内容?你的应用程序使用哪种类型的路由/导航库?你是否在
router.post
handling中设置服务器中的响应JSON?
中的@DrewReese
res
。然后((res)=>{\*code*}
part应该是一个纯布尔值,但执行永远不会到达
then
catch
,它会卡在
axios
函数中。正如@DrewReese所说。请尝试
res.json(resultTemp)
。在接收响应时,您是否应该使用
res.data===true
或其他方法?并尝试在发送时查看浏览器的“网络”选项卡中的内容request@Jerry我做了这两个更改,它成功了!非常感谢:)是
res
纯布尔值还是需要访问的响应对象?您是否检查过开发工具中的“网络”选项卡或将其记录下来以查看其实际内容?你的应用程序使用哪种类型的路由/导航库?你是否在
router.post
handling中设置服务器中的响应JSON?
中的@DrewReese
res
。然后((res)=>{\*code*}
part应该是一个纯布尔值,但执行永远不会到达
then
catch
,它会卡在
axios
函数中。正如@DrewReese所说。请尝试
res.json(resultTemp)
。在接收响应时,您是否应该使用
res.data===true
或其他方法?并尝试在发送时查看浏览器的“网络”选项卡中的内容request@Jerry我做了这两个改动,效果很好!非常感谢:)