如何解决节点 JS:类型错误:无法解构“未定义”或“空”的属性“access_token”
我目前正在从 Zoom Api 获取令牌,它工作正常。问题在于,当我尝试解构该属性以便获得 access_token 时。
var username = "client id";
var password = "client password";
var options = {
method: 'POST',url: 'https://zoom.us/oauth/token?grant_type=client_credentials',headers: {
/**The credential below is a sample base64 encoded credential. Replace it with "Authorization: 'Basic ' + Buffer.from(your_app_client_id + ':' + your_app_client_secret).toString('base64')"
**/
Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'),"Content-Type": "application/x-www-form-urlencoded"
}
};
const data = {
"topic": "Demo Meeting","type": 2,"start_time": "2020-05-05 12:00:00","password": "Hey123","agenda": "This is the meeting description","settings": {
"host_video": false,"participant_video": false,"join_before_host": false,"mute_upon_entry": true,"use_pmi": false,"approval_type": 0
}
};
function getResponse() {
return rp(options,function(error,response,body) {
if (error) throw new Error(error)
const { access_token } = response.data;
console.log(access_token)
}).then(function (req) {
//logic for your response
console.log("inside response",req);
return fetch("https://api.zoom.us/v2/users/me/meetings",{
method: "POST",headers: {
Authorization: `Bearer ${req}`,"Content-Type": "application/json"
},body: JSON.stringify(data)
});
})
.catch(function (err) {
// API call failed...
console.log('API call failed,reason ',err);
});
}
当我运行代码时,出现以下错误:TypeError:无法解构属性 access_token
of 'undefined' 或 'null'。
发生这种情况的任何原因?
解决方法
错误消息表示 response.data
为 undefined
。
将 console.log(response)
放入您的代码中,看看里面有什么,您会发现 data
属性丢失了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。