简体   繁体   English

我无法获取获取结果的信息。 承诺

[英]I can't take the information of my result of fetch. Promise

When I try to take the result of my API I can't.当我尝试获取我的 API 的结果时,我做不到。 my code:我的代码:

export async function validLoginFetch(email,password){

    const data = JSON.stringify({ email, password });
    let headers = {
      'Content-Type': 'application/json',
    };
  
    const options = {
      method: 'POST',
      body:data,
      headers:headers,
    }

    return await fetch('http://localhost:1323/profile/valid', options).then(response => {return response.json()})
}

the code that calls the function调用函数的代码

    async function validateLogin() {
       if (password !== "" && email !== "") {
          const response = await validLoginFetch(email, password)
          console.log(response)
      }
    };

the full code:完整代码:

import '../App.css';
import { validLoginFetch } from '../api/user'
import React , { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

export default () => {

  const [email, setEmail]  = useState("");
  const [password, setPassword]  = useState("");
  const [validUser, setValidUser]  = useState(false);

async function validateLogin() {
   if (password !== "" && email !== "") {
      const response = await validLoginFetch(email, password)
      console.log(response)
  }
};

  useEffect(()=> {
    if (validUser){
      //useNavigate("/#");

    }
  
  });


  return (
    <div className="login">
      <form onSubmit={validateLogin}>
        <div>
          <span>email</span>
          <input
            onChange={e => setEmail(e.target.value)}
            id="email"
            name="email"
            placeholder=""
          />
        </div>
        <div>
          <span>password</span>
          <input
            onChange={e => setPassword(e.target.value)}
            id="password"
            name="password"
            placeholder=""
          />
        </div>
        <button
          onSubmit={validLoginFetch}
          id="authenticate-user-login"
          className="authenticate-user-login"
        >
          Login
        </button>
      </form>
    </div>
);

}

not return anything.不返回任何东西。 My route works fine.我的路线运行良好。 The problem isn't in the backend.问题不在后端。

if I try to delete the async and await, the return is a Promise.如果我尝试删除异步并等待,则返回是一个 Promise。 If I use the async and await did never return anything.如果我使用 async 并且 await 永远不会返回任何东西。 it stays in the validLoginFetch and doesn't continue.它停留在validLoginFetch中并且不会继续。 Does someone know what it can be?有人知道它可能是什么吗?

You should use ASYNC/AWAIT or THEN, not both at the same time.您应该使用 ASYNC/AWAIT 或 THEN,而不是同时使用两者。

Try this:尝试这个:

export async function validLoginFetch(email,password){

    const data = JSON.stringify({ email, password });
    let headers = {
      'Content-Type': 'application/json',
    };
  
    const options = {
      method: 'POST',
      body:data,
      headers:headers,
    }

    const response = await fetch('http://localhost:1323/profile/valid', options);
    const json = await response.json();

    return json; 
}

I finded the answer.... we need to refresh the form before send it to fetch... so The problem was the time for refreshing the form, we need to refreshing before send it.我找到了答案......我们需要在发送之前刷新表单以获取......所以问题是刷​​新表单的时间,我们需要在发送之前刷新。 To solve this, set to refresh the form on response:要解决此问题,请设置在响应时刷新表单:

event.preventDefault();

in my case:就我而言:

resolve put these command before called the function that make the fetch:解决在调用进行提取的函数之前放置这些命令:

async function validateLogin() {
   event.preventDefault();
   if (password !== "" && email !== "") {
      const response = await validLoginFetch(email, password)
      console.log(response)
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM