简体   繁体   中英

request call giving me TypeError: Cannot read property 'statusCode' of undefined

I'm trying to use the OneDrive API to checkout a file (locking it by making it read-only) https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_checkout?view=odsp-graph-online

My lockFile function below calls this api, and successfully locks the file. The problem is that I'm getting an error right after the call: TypeError: Cannot read property 'statusCode' of undefined

I've been stepping through this call with my debugger and the error is popping before I even try to call statusCode on the line below it. Looking at the OneDrive API, it says it only returns a status: 204 . Could it be that the request package I'm using is expecting a statusCode somewhere under the hood? If so, how can I resolve this if I'm only getting a status back?

Also, while stepping through with my debugger I've noticed that the error pops up a second before I see the response returns and is set to IncomingMessage . I'm trying to look up docs explaining what that means, but I haven't been finding anything helpful.

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);
        
        });
};

I can't comment (NOOB), my guess without running the code is that you are returning the function before code inside the if statement is finished executing.

try adding an else block and put the return there. I typically surround things in a promise or at the least a try catch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM