Node.js 等待不是等待等待等待的有效钥匙中的钥匙
这对我来说是出乎意料的,但这似乎是async Wait本身的一些问题。我使用的是节点v8.0 有关以下代码:Node.js 等待不是等待等待等待的有效钥匙中的钥匙,node.js,asynchronous,async-await,Node.js,Asynchronous,Async Await,这对我来说是出乎意料的,但这似乎是async Wait本身的一些问题。我使用的是节点v8.0 有关以下代码: (async function(){ var userProfile = await (myApiHelper.getUserProfile(userName)); console.log(userProfile); console.log(userProfile.location); }()); 在上述代码中 console.log(userProfile) ,打印正确
(async function(){
var userProfile = await (myApiHelper.getUserProfile(userName));
console.log(userProfile);
console.log(userProfile.location);
}());
在上述代码中
console.log(userProfile)
,打印正确的数据:
{“姓名”:“abc”,“地点”:“班加罗尔”}
但是
打印未定义
如果我做错了什么,有人能帮我吗。
userProfile
是字符串吗?我们能看看getUserProfile
是如何工作的吗?它返回JSON吗?谢谢@MukeshSharma和AndrewLi,默认情况下request.get返回字符串值。当我传递json:true时,它开始返回Object。我的问题解决了。
console.log(userProfile.location)