Promise链取回Array(0)

Promise chain fetch back Array (0)

我试过 promise 链,其中我的链 var 是有效的,我的 fetch 正在处理手动键入的数据,但我无法通过将var, return 控制台日志显示空白值 {"code":"success","message":null,"data":[]}{"code":"success","message":null,"data":[Array(0)]}。 关于我在代码中做错了什么有什么建议吗?

function anExportFunction(){
  fetch(an_API_URL_01,{
    method: 'GET',
}).then(function(response) {
  return response.text();
})
.then(function(dataIds) {
  return fetch(an_API_URL_02,{
    method: 'POST',
    body: JSON.stringify({
      "elementIds" : ['dataIds'],
    })
})
.then(response => response.text())
.then(data=> console.log(data))
});

因此,使此获取响应与服务器一起工作的手动输入如下 "elementIds" : ["0001","0002","0003",...]

console.log(dataIds)相当于{"code":"success","message":null,"data":["0001","0002","0003",...]}

根据@Nick 和@Nisala 提供的建议,我成功地完成了一些 twerk!感谢两位专家提供想法,为成功解决此问题做出贡献!

问题的关键在于response从服务提供商服务器反馈的总是由几个标准组成,包括前面提到的代码,消息和数据{"code":"success","message":null,"data":["0001","0002","0003",...]}

为了只提供准确的 data 内容,必须过滤掉不需要的数据,并确保 JSON 内容提供第二个 fetch 请求,而不是 JSON.stringify内容。

因此,Promise Chain 应该构建如下

function anExportFunction(){
  fetch(an_API_URL_01,{
    method: 'GET',
}).then(response => response.json())
.then(retrieveId => {
    const dataIds = retrieveId.data;
    return fetch(an_API_URL_02,{
    method: 'POST',
    body: JSON.stringify({
      "elementIds" : dataIds,
    })
})
.then(response => response.text())
.then(data=> console.log(data))
});