f*****w 发帖数: 2602 | 1 入门问题,肯定是我什么地方理解错了 但是不明白为啥
这样的两行代码
console.log('req.body:' + JSON.stringify(req.body))
console.log('email:' + req.body['email'])
输出是:
req.body:{"body":{"email":"[email protected]
/* */"}}
email:undefined
我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他
的reference的方法,都是说undefined
请问这是为啥啊? |
p*****2 发帖数: 21240 | 2 看起来body有两层呀
/* */"}}
【在 f*****w 的大作中提到】 : 入门问题,肯定是我什么地方理解错了 但是不明白为啥 : 这样的两行代码 : console.log('req.body:' + JSON.stringify(req.body)) : console.log('email:' + req.body['email']) : 输出是: : req.body:{"body":{"email":"[email protected] : /* */"}} : email:undefined : 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他 : 的reference的方法,都是说undefined
|
f*****w 发帖数: 2602 | 3 body之下还有两层? 我看上去只有一层啊
难道不是req.body.email就到leaf了么
【在 p*****2 的大作中提到】 : 看起来body有两层呀 : : /* */"}}
|
f*****w 发帖数: 2602 | 4 ah ... 明白了 stupid
【在 p*****2 的大作中提到】 : 看起来body有两层呀 : : /* */"}}
|
g*********e 发帖数: 14401 | 5 {email:"dsf@dfrd"}这个object没法直接print吧? |
n*****t 发帖数: 22014 | 6 body.body.email
【在 f*****w 的大作中提到】 : body之下还有两层? 我看上去只有一层啊 : 难道不是req.body.email就到leaf了么
|
l**********n 发帖数: 8443 | 7 这不明显吗?这也来问。
/* */"}}
【在 f*****w 的大作中提到】 : 入门问题,肯定是我什么地方理解错了 但是不明白为啥 : 这样的两行代码 : console.log('req.body:' + JSON.stringify(req.body)) : console.log('email:' + req.body['email']) : 输出是: : req.body:{"body":{"email":"[email protected] : /* */"}} : email:undefined : 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他 : 的reference的方法,都是说undefined
|
p*****2 发帖数: 21240 | 8 人家说了是入门呀
【在 l**********n 的大作中提到】 : 这不明显吗?这也来问。 : : /* */"}}
|
f*****w 发帖数: 2602 | 9 不好意思啊 浪费大家时间了
新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的
是上一层的变量
及其蠢的问题。。。
非常感谢大家回复
【在 l**********n 的大作中提到】 : 这不明显吗?这也来问。 : : /* */"}}
|
p*****2 发帖数: 21240 | 10 这种问题常见 不算什么
老手有时候也会犯类似问题
【在 f*****w 的大作中提到】 : 不好意思啊 浪费大家时间了 : 新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的 : 是上一层的变量 : 及其蠢的问题。。。 : 非常感谢大家回复
|
|
|
W***o 发帖数: 6519 | 11 to get email, it should be req.body.body.email
ps: does it wear a ma-jia ?
/* */"}}
【在 f*****w 的大作中提到】 : 入门问题,肯定是我什么地方理解错了 但是不明白为啥 : 这样的两行代码 : console.log('req.body:' + JSON.stringify(req.body)) : console.log('email:' + req.body['email']) : 输出是: : req.body:{"body":{"email":"[email protected] : /* */"}} : email:undefined : 我不明白为什么 ,明明我在body的内容里面是有email这个属性的。而且我尝试了其他 : 的reference的方法,都是说undefined
|
l**********n 发帖数: 8443 | 12 这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写
req.body.body, 这样写法不好,万一返回的格式变了,又要重写。
【在 f*****w 的大作中提到】 : 不好意思啊 浪费大家时间了 : 新手。。。以为自己访问方法不对, body里面有body因为名字完全一样还以为输出的 : 是上一层的变量 : 及其蠢的问题。。。 : 非常感谢大家回复
|
l**********n 发帖数: 8443 | 13 如果是scala, 这种情况就不会出现。
【在 l**********n 的大作中提到】 : 这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写 : req.body.body, 这样写法不好,万一返回的格式变了,又要重写。
|
f*****w 发帖数: 2602 | 14 没很明白 能展开说说吗
【在 l**********n 的大作中提到】 : 这种情况我会加一层,把要的剥离出来,以表明这是返回的格式脑残,总之我不会写 : req.body.body, 这样写法不好,万一返回的格式变了,又要重写。
|
l**********n 发帖数: 8443 | 15 functional programming, 就是data transformation, functions are very easy to
test.
the idea is before you process the response received from the server side.
do some pre-processing on them, like sanity check, even you can throw Error
if the response is not what you expect.
The idea is that you just pass a Promise around.
A Promise has three state: unresolved, resolved, and rejected.
There are many ways to create a Promise, using a constructor,
new Promise(function(resolve, reject){
// do your processing here, once processing done
// you can do
resolve(value)
// or
reject(reason)
})
You can directly do:
Promise.resolve(value)
or Promise.reject(reason)
If something is a thenable, you can do:
Promise.resolve(thenable)
finally you can do
promise.then(function(value){
})
.catch(function(err){
})
you can even catch specific errors with the help of bluebird.
Because I know the response has been sanity checked, I can safely use it.
Also Promise holds the exception too so I can do something else if something
went wrong.
【在 f*****w 的大作中提到】 : 没很明白 能展开说说吗
|
p*****2 发帖数: 21240 | 16
to
Error
req?.body?.body
【在 l**********n 的大作中提到】 : functional programming, 就是data transformation, functions are very easy to : test. : the idea is before you process the response received from the server side. : do some pre-processing on them, like sanity check, even you can throw Error : if the response is not what you expect. : The idea is that you just pass a Promise around. : A Promise has three state: unresolved, resolved, and rejected. : There are many ways to create a Promise, using a constructor, : new Promise(function(resolve, reject){ : // do your processing here, once processing done
|
l**********n 发帖数: 8443 | 17 how you process exception in your way?
Scala has Promise too.
【在 p*****2 的大作中提到】 : : to : Error : req?.body?.body
|
L***s 发帖数: 1148 | 18
那个是coffeescript写法,跟exception没关系
x = req?.body?.body 编译成js就是一坨
x = typeof req !== 'undefined' && req !== null ?
( (ref = req.body) != null ? ref.body : void 0 ) : void 0;
【在 l**********n 的大作中提到】 : how you process exception in your way? : Scala has Promise too.
|
p*****2 发帖数: 21240 | 19
还是大牛聪明
【在 L***s 的大作中提到】 : : 那个是coffeescript写法,跟exception没关系 : x = req?.body?.body 编译成js就是一坨 : x = typeof req !== 'undefined' && req !== null ? : ( (ref = req.body) != null ? ref.body : void 0 ) : void 0;
|
L***s 发帖数: 1148 | 20
我只是帮二爷做做注脚
【在 p*****2 的大作中提到】 : : 还是大牛聪明
|