Asynchronous Axios Get请求出现问题-可能是异步问题

Asynchronous Axios Get请求出现问题-可能是异步问题,asynchronous,promise,async-await,axios,sequelize.js,Asynchronous,Promise,Async Await,Axios,Sequelize.js,我似乎有异步问题。我在整个应用程序中使用react、express、sequelize和mariadb。我在前端使用axios发出get请求。但是,get请求始终返回空值。但是,在我的后端代码中,我知道请求正在调用数据库findAll() 前端(React/Axios) server.js app.get('/getdogs', (req, res) => { console.log("IN THE FUNCTION"); const pets = db.get

我似乎有异步问题。我在整个应用程序中使用react、express、sequelize和mariadb。我在前端使用axios发出get请求。但是,get请求始终返回空值。但是,在我的后端代码中,我知道请求正在调用数据库findAll()

前端(React/Axios)

server.js

app.get('/getdogs', (req, res) => {
  console.log("IN THE FUNCTION");
  const pets = db.getPets();
  console.log("All pets:", JSON.stringify(pets, null, 2));

  res.send(pets);
});
nodemon] starting `node server.js`
Listening on port 5000
IN THE FUNCTION
All pets: {}
warning: please use IANA standard timezone format ('Etc/GMT0')
warning: please use IANA standard timezone format ('Etc/GMT0')
Executing (default): SELECT `id`, `name`, `createdAt`, `updatedAt` FROM `Pets` AS `Pets`;
All pets: [
  {
    "id": 1,
    "name": "HULK",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  },
  {
    "id": 2,
    "name": "Martha",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  },
  {
    "id": 3,
    "name": "Bernie",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  }
]

database.js

async function getPets() {
  const pets = await Pets.findAll();
  console.log("All pets:", JSON.stringify(pets, null, 2));
  return JSON.stringify(pets, null, 2);
}
server.js的输出

nodemon] starting `node server.js`
Listening on port 5000
IN THE FUNCTION
All pets: {}
warning: please use IANA standard timezone format ('Etc/GMT0')
warning: please use IANA standard timezone format ('Etc/GMT0')
Executing (default): SELECT `id`, `name`, `createdAt`, `updatedAt` FROM `Pets` AS `Pets`;
All pets: [
  {
    "id": 1,
    "name": "HULK",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  },
  {
    "id": 2,
    "name": "Martha",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  },
  {
    "id": 3,
    "name": "Bernie",
    "createdAt": "2020-09-15T23:09:43.000Z",
    "updatedAt": "2020-09-15T23:09:43.000Z"
  }
]

  • axios响应没有
    主体
    属性。响应位于
    数据
    属性中,请参阅
  • 您尚未等待DB的结果:

  • 谢谢我只是在读关于使用的书,然后是关于等待的承诺。
     this.setState({ pets: resp.data });
            console.log(resp.data);
    
    db.getPets().then(pets => {
      console.log("All pets:", JSON.stringify(pets, null, 2));
    
      res.send(pets);
    });