阅读获取承诺的主体 [英] Read the body of a Fetch Promise

查看:68
本文介绍了阅读获取承诺的主体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这有一个简单的答案,但对于我的生活,我无法弄清楚如何去做。



我有以下用于上传到Google云端存储的快速端点。它工作得很好,谷歌api的响应给了我一个独特的文件名,我想传回我的前端:

  app.post('/ upload',(req,res)=> {
var form = new formidable.IncomingForm(),
files = [],
fields = [] ;

形式
.on('field',函数(字段,值){
fields.push([field,value]);
})
.on('file',function(field,file){
files.push([field,file]);
})
.on('end',function (){
console.log(' - > upload done');
});
form.parse(req,function(err,fields,files){
var filePath = files.file.path;
bucket.upload(filePath,function(err,file,apiResponse){
if(!err){
res.writeHead(200,{' content-type':'text / plain'});
res.end(唯一文件名:+ file.name);
} else {
res.writeHead(500) ;
res.end();
}
});
});

返回;
});

我通过调用一个将文件传递给它的短函数来到达这个端点:

 函数上传(文件){
var data = new FormData();
data.append('file',file);
返回fetch(`upload`,{
方法:'POST',
body:data
});
}

const Client = {upload};
导出默认客户端;

此函数从我的前端调用如下:

  Client.upload(this.file).then((data)=> {
console.log(data);
});

此最终 console.log(数据)将响应记录到控制台。但是,我没有看到我写的响应(唯一文件名:+ file.name



有没有人对如何从客户端的响应正文中检索此信息有任何建议?



编辑:



数据看起来像这样当我在console.log上时:



解决方案

请注意,您正在处理响应对象。您需要基本读取响应流 Response.json() Response.text()(或通过其他方法)以查看您的数据。否则,您的响应正文将始终显示为锁定的可读流。例如:

  fetch('https://api.ipify.org?format=json')
。然后(response => response.json())
.then(data => {console.log(data);})

如果这会给您带来意想不到的结果,您可能需要使用检查您的回复邮差


I'm sure this has a simple answer, but for the life of me I can't figure out how to do it.

I have the following express endpoint for uploading to Google Cloud storage. It works great and the response from the google api gives me a unique file name that I want to pass back to my front end:

app.post('/upload', (req, res) => {
  var form = new formidable.IncomingForm(),
  files = [],
  fields = [];

  form
    .on('field', function(field, value) {
      fields.push([field, value]);
    })
    .on('file', function(field, file) {
      files.push([field, file]);
    })
    .on('end', function() {
      console.log('-> upload done');
    });
  form.parse(req, function(err, fields, files){
    var filePath = files.file.path;
    bucket.upload(filePath, function(err, file, apiResponse){
      if (!err){
        res.writeHead(200, {'content-type': 'text/plain'});
        res.end("Unique File Name:" + file.name);
      }else{
        res.writeHead(500);
        res.end();
      }
    });
  });

 return;
});

I reach this endpoint by calling a short function which passes the file to it:

function upload(file) {
  var data = new FormData();
  data.append('file', file);
  return fetch(`upload`,{
    method: 'POST',
    body: data
  });
}

const Client = { upload };
export default Client;

This function is called from my front end like this:

Client.upload(this.file).then((data) => {
  console.log(data);
});

This final console.log(data) logs the response to the console. However, I don't see anywhere the response that I wrote in ("Unique File Name:" + file.name)

Does anyone have any suggestions for how I can retrieve this info from the response body on the client side?

EDIT:

The data looks like this when I console.log it:

EDIT 2:

This is the response I get when I POST a file to my endpoint using Postman:

解决方案

Notice you're dealing with a Response object. You need to basically read the response stream with Response.json() or Response.text() (or via other methods) in order to see your data. Otherwise your response body will always appear as a locked readable stream. For example:

fetch('https://api.ipify.org?format=json')
.then(response=>response.json())
.then‌​(data=>{ console.log(data); })

If this gives you unexpected results, you may want to inspect your response with Postman.

这篇关于阅读获取承诺的主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆