[英]protractor throwing 'TypeError: Cannot read property 'statusCode' of undefined' on request callback
[英]request call giving me TypeError: Cannot read property 'statusCode' of undefined
我正在嘗試使用 OneDrive API 簽出文件(通過使其成為只讀來鎖定它) https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_checkout?view =odsp-graph-在線
我下面的lockFile
function調用這個api,成功鎖定文件。 問題是我在調用后立即收到錯誤: TypeError: Cannot read property 'statusCode' of undefined
我一直在用我的調試器單步執行這個調用,並且在我嘗試調用它下面的行上的 statusCode 之前,錯誤就出現了。 查看 OneDrive API,它說它只返回一個status: 204
。 難道我正在使用的請求statusCode
期望在引擎蓋下的某個地方有一個狀態代碼? 如果是這樣,如果我只是恢復status
,我該如何解決這個問題?
此外,在使用我的調試器單步執行時,我注意到在我看到response
返回並設置為IncomingMessage
之前一秒鍾會彈出錯誤。 我正在嘗試查找解釋這意味着什么的文檔,但我沒有找到任何有用的東西。
lockFile(fileId, driveId, authorization, callback) {
const call = {
method: 'POST',
url: authorization.meta.serviceEndpointUri + '/drives/' + driveId + '/items/' + fileId + '/checkout',
headers: {
Authorization: 'Bearer ' + authorization.access_token
}
}
request(call, (err, response, body) => {
/////////////////
//I'M GETTING THE ERROR RIGHT HERE, BEFORE IT GETS TO THE NEXT LINE
/////////////////
if (err || (response && response.statusCode !== 204)) {
let statusCode = (response && response.statusCode) ? response.statusCode : 500;
statusCode = (statusCode === 204) ? 500 : statusCode;//We don't want to send a 204 id there isn't the right data
callback(StorageProviderError(this.name, statusCode, 1007, body));
}
return callback(null, response);
});
};
我無法評論(NOOB),如果不運行代碼,我的猜測是您在 if 語句中的代碼完成執行之前返回 function 。
嘗試添加一個 else 塊並將 return 放在那里。 我通常將事物包圍在 promise 或至少一個 try catch 塊中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.