无法从JavaScript对象中读取值,始终为undefined。

huangapple go评论62阅读模式
英文:

Cant read vaules from JavaScript Object always undifined

问题

I can't retrieve values from a JavaScript object.

I have tried several ways but I can't get it to work the usual way i now but i am pretty new in javaScript.

Thanks for your time and attention, if there is anything missing or you need some more information please contact me.

The Object form console.log(unpackedReq)

{
 'mFile ': {
    name: 'test',
    data: { type: 'Buffer', data: [Array] },
    size: 4,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'application/octet-stream',
    md5: '098f6bcd4621d373cade4e832627b4f6'
  }
}

My Code:

app.post('/single', async (req, res, next) => {
try {
    
    const unpackedReq = JSON.parse(JSON.stringify(req.files));
    const file = unpackedReq.mFile

    console.log(unpackedReq)
    console.log(typeof(unpackedReq)) //object
    console.log(unpackedReq["mFile"].name) // TypeError: Cannot read properties of undefined (reading 'name')


} catch (error) {
    console.log(error)
    res.send('Error uploading file')
}

The Full error:

> TypeError: Cannot read properties of undefined (reading 'name')
>     at /Users/lukasbronstering/VsCode/ftp-web-server/app.js:25:42
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)
>     at next (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/route.js:144:13)
>     at Route.dispatch (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/route.js:114:3)
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)
>     at /Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:284:15
>     at Function.process_params (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:346:12)
>     at next (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:280:10)
>     at jsonParser (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/body-parser/lib/types/json.js:119:7)
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)
英文:

I can't retrieve values from a JavaScript object.

I have tried several ways but I can't get it to work the usual way i now but i am pretty new in javaScript.

Thanks for your time and attention, if there is anything missing or you need some more information please contact me.

The Object form consol.log(unpackedReq)

{
 'mFile ': {
    name: 'test',
    data: { type: 'Buffer', data: [Array] },
    size: 4,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'application/octet-stream',
    md5: '098f6bcd4621d373cade4e832627b4f6'
  }
}

My Code:

app.post('/single', async(req, res, next) => {
try {
    
    const unpackedReq = JSON.parse(JSON.stringify(req.files));
    const file = unpackedReq.mFile

    console.log(unpackedReq)
    console.log(typeof(unpackedReq)) //object
    console.log(unpackedReq["mFile"].name) // TypeError: Cannot read properties of undefined (reading 'name')


} catch (error) {
    console.log(error)
    res.send('Error uploading file')
}

The Full error:

> TypeError: Cannot read properties of undefined (reading 'name')
>     at /Users/lukasbronstering/VsCode/ftp-web-server/app.js:25:42
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)
>     at next (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/route.js:144:13)
>     at Route.dispatch (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/route.js:114:3)
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)
>     at /Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:284:15
>     at Function.process_params (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:346:12)
>     at next (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/index.js:280:10)
>     at jsonParser (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/body-parser/lib/types/json.js:119:7)
>     at Layer.handle [as handle_request] (/Users/lukasbronstering/VsCode/ftp-web-server/node_modules/express/lib/router/layer.js:95:5)

答案1

得分: 2

对象'mFile '中有一个空格,但您在没有空格的情况下访问它。

因此,您可以在访问对象时添加空格,就像这样:console.log(unpackedReq["mFile "].name),或者您可以更改对象。但我建议更改对象名称。

英文:

There is a space in object 'mFile ' and you're accessing it without space.

so you can just add the space while accessing your object like this console.log(unpackedReq["mFile "].name) or you could change your object. but i would recommend to change your object name

huangapple
  • 本文由 发表于 2023年2月10日 10:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定