JavaScript中为什么获取不到对象中的值

编辑了,评论区两位的答案帮我解决了。之前不知道对象后续被附加了属性之后,之前log的结果也会发生改变。

const a = {}
console.log(a) // {name:1}
a.name = 1

所以我用了一叶知秋同学的方法,将此刻的状态打印出来.

console.log(JSON.parse(JSON.stringify(info.fileList[0]))
// 这样打印出来的就是log这一刻的对象属性,发现是没有reseponse的

所以我觉得是我判断条件的问题,查看了一下这个upload组件的API,果然有一个字段是status,所以我通过status判断上传状态就能打出来了。(这也说明process这个属性只是用于支持显示进度,真正的上传成功回调在这之后发生)

if (info.hasOwnProperty('file') && info.file.status === 'done') {
    console.log(info.fileList[0])
    console.log(info.fileList[0].response)
}

以下是原问题


问题代码

// 上传确认的文件
handleUpload (info) {
  if (info.hasOwnProperty('event') && info.event.percent == 100) {
    console.log(info)
    console.log(info.fileList)
    console.log(info.fileList[0])
    console.log(info.fileList[0].response)
    // const fileId = info.fileList[0].response.data.fileId  // Error data of undefined
    // this.uploadRow.fileResult = fileId
    // this.getFileInfo(this.uploadRow)
  }
},

获取上传文件的结果,但是这个response获取就是为undefined,请问有可能是什么原因导致的,有什么debug思路。
这里是上面代码的输出,显然这个结果中有response对象,但是我后来尝试了输出'response' in info.fileList[0]结果为false
WechatIMG195.png

阅读 6.6k
2 个回答

你这个是progress事件,是上传中的事件吧。response是完成后附加上去的吧?

打印是什么还真不一定就是什么,尤其是对象这种引用类型,属性后附加的就会出现展开看有属性,但是打印的当时是没有的情况:
豆芽图片20191211195250.png

typeof一下看下类型info.fileList[0] 如果是String的话 JOSN格式化一下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题