回调 return 响应但是当发送 API 响应时在节点 JS 中显示空白

Callback return the response but when send in API response shows Blank in node JS

我必须使用 REST API 发送响应。回调 return 确切的响应,但是当我将它传递给 API 响应时,它显示空白。

代码如下:

getAuctionListingByType(filterData, function(response) {
  console.log('auction listing');
  console.log(response);
  res.json({
    code: 200,
    message: 'Auction',
    data: response
  });
});

控制台显示以下响应:

 [[auction_id: '3',title: 'Salvage Auction 2011',business: 'Insurance'],
 [auction_id: '4',title: 'Salvage Auction 2013',business: 'Bank']]

但是当我点击 API 时,响应是:

{"code":200,"message":"Auction","data":[[],[]]}

当我控制它打印的响应时,但是当我传递给客户端时它是空白的。

将响应存储在变量中并将该变量传递给 api。

       getAuctionListingByType(filterData, function(response) {
              console.log('auction listing');
              console.log(response);
              var TempStore=response;
             res.json({
                   code: 200,
                   message: 'Auction',
                  data: TempStore
                  });
             });

您正在从服务器发送 array of arrays,但您的 array 无效。

Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.

您正在将 object 视为 array

响应应如下所示:

[{auction_id: '3',title: 'Salvage Auction 2011',business: 'Insurance'},{auction_id: '4',title: 'Salvage Auction 2013',business: 'Bank'}];

注意:检查控制台(cmd)是否有错误,您可能会看到Unexpected token :

您的回复应采用对象数组格式,而不是数组格式。

您的回复应该是这样的:

[{auction_id: '3',title: 'Salvage Auction 2011',business: 'Insurance'},{auction_id: '4',title: 'Salvage Auction 2013',business: 'Bank'}]

更改您对对象数组的响应格式。