客户端的空对象。如何正确传递promises值?

时间:2017-12-18 09:14:23

标签: javascript reactjs promise client-server

我遇到了包含promises值的对象以及在Cilent端访问它们的问题。我希望能够访问这些值,但我收到的只是空对象。

以下是服务器端的代码:

router.get('/toExcess', function(req, res){
  console.log(req.query);  
  const response = logic.adder(req.query.a, req.query.b);
  const convertToModular = logic.modularRecordExcess(response);
  const convertToOpposite = logic.oppositeRecordExcess(response);
  const convertToObjective = logic.objectiveRecordExcess(response);
  convertToObjective.then(function (response) {
   var add = {
   modular: convertToModular,
   opposite: convertToOpposite,
   objective: convertToObjective,
 }
 console.log(add);
 res.send(add);
}).catch(function(err){

});
});

它是服务器端的控制台日志:

 { modular: Promise { [ '1.', '0111', '.', '1001', '0011' ] },
  opposite: Promise { [ '1.', '1000', '.', '0110', '1100' ] },
  objective: Promise { [ '1.', '1000', '.', '0110', '1101' ] } }

这里是反应函数:

showExcess(){
  document.querySelector('.input-wrapper').style.display = 'inline';
  RestClient.getExcess(this.state.augend, this.state.addend).then((response) 
  => {    
this.setState({
      isReady: true
  });
      console.log(response);
  });
}

和浏览器中的consol.log结果: enter image description here

对象为空。 有什么建议?我需要访问模块化,客观和相反的值。

1 个答案:

答案 0 :(得分:0)

router.get('/toExcess', function(req, res){
  console.log(req.query);  
  const response = logic.adder(req.query.a, req.query.b);
  const convertToModular = logic.modularRecordExcess(response);
  const convertToOpposite = logic.oppositeRecordExcess(response);
  const convertToObjective = logic.objectiveRecordExcess(response);
  Promise.all([convertToModular, convertToOpposite, convertToObjective])
  .then(function (response) {
   var add = {
      modular: response[0],
      opposite: response[1],
      objective: response[2],
   }
  console.log(add);
  res.send(add);
  //or you can pass res.json(response);
  })
 .catch(function(err){

 });
});