dimer47 / node-ovh-objectstorage

Creative Commons Attribution Share Alike 4.0 International
12 stars 11 forks source link

Sometimes storage.object().get() could return "Cannot read property 'headers' of undefined" #1

Closed deejaybill closed 7 years ago

deejaybill commented 7 years ago

Sometimes when I use storage.object().get() in a chain of promises it could return the following error "Cannot read property 'headers' of undefined".

TypeError: Cannot read property 'headers' of undefined
    at WriteStream.<anonymous> ([...]\node_modules\node-ovh-objectstorage\lib\OVHObjectStorage.js:257:48)
    at emitNone (events.js:110:20)
    at WriteStream.emit (events.js:207:7)
    at finishMaybe (_stream_writable.js:579:14)
    at afterWrite (_stream_writable.js:431:3)
    at onwrite (_stream_writable.js:421:7)
    at fs.js:2133:5
    at FSReqWrap.wrapper [as oncomplete] (fs.js:690:5)

The problem I think is at the line 247 of the OVHObjectStorage.js file. If I change the data object with three variables, it always works perfectly. I know this is pretty nonsense but the code below doesn't get me any error.

        try {
          var targetURL = _this.endpoint.publicURL + path;
          var writeStream = fs.createWriteStream(pathLocal);
          // var data = {};
          var get_err = {};
          var get_res = {};
          var get_body = "";
          var r = request({
            method:   'GET',
            uri:      targetURL,
            headers:  {
              "X-Auth-Token": _this.token.id,
              "Accept": "application/json"
            }
          }, function (err, res, body) {
             get_err = err; 
             get_res = res; 
             get_body = body;
            }).pipe(writeStream);
          writeStream.on('error', function (err) {
            return failback(err);
          });
          writeStream.on('finish', function() {
            return callback(get_body, get_res.headers);
            // return callback(data.body, data.res.headers);
          });
dimer47 commented 7 years ago

Hello @deejaybill,

I apologize for the delay in processing this error.

Indeed I see this error well, I have not used the API enough to have seen it in the past. In order to correct it, I prefix the variables as you advised me but also I recall the method if an error during the query occurs.

Thank you for having postponed this problem, if any time you notice any other error , please let me know.

Regards, Dimer47