Javascript 使用react和Express.js获取API响应;t使用console.log显示任何结果

Javascript 使用react和Express.js获取API响应;t使用console.log显示任何结果,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我有一个登录表单,它使用fetch将数据发送到Express.js后端。在客户端,当我想在fetch调用完成时显示其结果时,不会显示任何内容(而且它永远不会到达数据回调)。我似乎没有收到任何错误,但我知道数据已成功发送到后端 以下是Express.js服务器代码: const express = require('express'); const User = express.Router(); const bcrypt = require('bcrypt'); const user =

我有一个登录表单,它使用
fetch
将数据发送到Express.js后端。在客户端,当我想在fetch调用完成时显示其结果时,不会显示任何内容(而且它永远不会到达数据回调)。我似乎没有收到任何错误,但我知道数据已成功发送到后端

以下是Express.js服务器代码:

const express = require('express');

const User = express.Router();

const bcrypt = require('bcrypt');

const user = require('../Models/user');
这是经过编辑的

下面是获取调用:

fetch('http://localhost:4000/login',{
  method: 'POST',
  headers: {
    'Accept': 'application/json,text/plain, */*',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    userName: this.state.userName,
    password: this.state.password,

  }),

}).then((response)=>{
if(response.ok){
  console.log(response)
}
else {
  console.log("a problem")
}
}).then((data)=>{
console.log(data)
});

从response.ok中删除
ok

Remove.then((数据)=>{console.log(数据)})

并检查控制台日志

}).then((response)=>{
if(response){
  console.log(response)
}
else {
  console.log("a problem")
}
}).then((data)=>{
console.log(data)
});

从response.ok中删除
ok

Remove.then((数据)=>{console.log(数据)})

并检查控制台日志

}).then((response)=>{
if(response){
  console.log(response)
}
else {
  console.log("a problem")
}
}).then((data)=>{
console.log(data)
});

在您的
loginRouteHandler
中,如果bcrypt比较成功,则响应中不会返回任何内容。因此,在if语句的第一个分支中,放入
res.send('Success!')
或类似的内容

下面是一个例子:

function loginRouteHandler(req, res) {
  user.findOne(
    {
      where: {
        userName: req.body.userName,
      },
    },
  )
    .then((data) => {
      if (bcrypt.compareSync(req.body.password, data.password)) {
        req.session.userName = req.body.userName;
        req.session.password = req.body.password;
        console.log(req.session);
        res.status(200).send('Success!');
      } else {
        res.status(400).send('some text');
      }
    });
}
更新:使用
.text()
.json()
也无法获得获取响应的输出。您必须将fetch调用更新为以下内容:

fetch(/* stuff */).then((response)=>{
if(response.ok){
  console.log(response)
}
else {
  console.log("a problem")
}
return response.text()
}).then((data)=>{
console.log(data)
});

在您的
loginRouteHandler
中,如果bcrypt比较成功,则响应中不会返回任何内容。因此,在if语句的第一个分支中,放入
res.send('Success!')
或类似的内容

下面是一个例子:

function loginRouteHandler(req, res) {
  user.findOne(
    {
      where: {
        userName: req.body.userName,
      },
    },
  )
    .then((data) => {
      if (bcrypt.compareSync(req.body.password, data.password)) {
        req.session.userName = req.body.userName;
        req.session.password = req.body.password;
        console.log(req.session);
        res.status(200).send('Success!');
      } else {
        res.status(400).send('some text');
      }
    });
}
更新:使用
.text()
.json()
也无法获得获取响应的输出。您必须将fetch调用更新为以下内容:

fetch(/* stuff */).then((response)=>{
if(response.ok){
  console.log(response)
}
else {
  console.log("a problem")
}
return response.text()
}).then((data)=>{
console.log(data)
});

请不要只说“它失败了”,我们能得到准确的错误信息或预期/实际结果是什么吗?谢谢。在
fetch
的第一个
then()
中没有返回任何内容,要在第二个
中传递给
data
参数
then()
是有意义的,我建议进行编辑。请不要只说“它失败了”,我们能得到准确的错误消息或预期/实际结果是什么吗?谢谢。在
fetch
的第一个
then()
中没有返回任何内容,要在第二个
data
参数中传递给
then()
有意义,我建议进行编辑。谢谢我修改了代码,它在第一个控制台中给出了未定义的内容。然后将
return response.text()
放在末尾。否则,第一个参数将未定义。我正在更新我的答案。你可能还应该添加一个
.catch()
子句,并抛出一个错误,而不是控制台。记录“问题”你是对的,谢谢你。我将重新编写我的代码谢谢我修改了代码,它首先在控制台上给出了未定义的代码。然后在最后放
return response.text()
。否则,第一个参数将未定义。我正在更新我的答案。你可能还应该添加一个
.catch()
子句,并抛出一个错误,而不是控制台。记录“一个问题”你是对的,谢谢你我要重新编写我的代码非常感谢你,现在它在控制台上给了我未定义的答案非常感谢,现在它在控制台上给了我未定义的答案