英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论