【发布时间】:2021-06-17 21:05:56
【问题描述】:
我正在尝试发送一些 JSON 的 url,当我将某些内容上传到 azure blob 存储时。对象是req.files。当我console.logreq.files 时,我得到类似这样的东西:
[
{
fieldname: 'chooseFile',
originalname: 'video.mov',
encoding: '7bit',
mimetype: 'video/quicktime',
container: 'asset-afe1b5d0-cf8f-11eb-9615-13b96d691770',
blobPath: '1623950892848-aGxhZC5tb3Y%3D.mov',
url: 'https://cdn.example.com/item/this/that'
}
]
如果我尝试说,console.logreq.files.url,我会收到类似这样的错误:
Property 'url' does not exist on type '{ [fieldname: string]: File[]; } | File[]'.
Property 'url' does not exist on type 'File[]'
所以我试过这个:
const filedata = JSON.stringify(req.files)
console.log(filedata)
它返回更像这样的东西:
[{"fieldname":"chooseFile","originalname":"video.mov","encoding":"7bit","mimetype":"video/quicktime","container":"path","blobPath":"item.mov","url":"https://cdn.example.net/this/path/here/there"}]
现在如果我尝试console.logfiledata.url,我会收到如下错误:
Property 'url' does not exist on type 'string'
【问题讨论】:
-
filedata是一个字符串。这就是stringify返回的内容。而且,req.files是一个数组 -
如您所见,您正在记录的是一个数组,因此您应该执行以下操作:
console.log(filedata[0].url)... 但最重要的是,您可能想要使用而不是JSON.stringifyJSON.parse -
req.files似乎是对象的集合(数组),每个对象代表一个文件。您需要注意您尝试访问的文件,如下所示:req.files[0].url
标签: javascript json typescript