通过节点js上传到Dropbox失败

时间:2014-06-26 09:38:19

标签: javascript node.js dropbox dropbox-api

我有以下代码从流中读取并上传到Dropbox。但是我得到了一个

Uncaught TypeError: Cannot call method 'toString' of undefined
  at Function.Dropbox.Util.Xhr.Xhr.urlEncodeValue (node_modules\dropbox\lib\dropbox.js:3695:40)
  at Function.Dropbox.Util.Xhr.Xhr.urlEncode (node_modules\dropbox\lib\dropbox.js:3689:59)
  at Xhr.Dropbox.Util.Xhr.Xhr.paramsToUrl (node_modules\dropbox\lib\dropbox.js:3570:40)
  at Xhr.Dropbox.Util.Xhr.Xhr.prepare (node_modules\dropbox\lib\dropbox.js:3598:14)
  at Client.Dropbox.Client.Client._dispatchXhr (node_modules\dropbox\lib\dropbox.js:2137:11)
  at Client.Dropbox.Client.Client.resumableUploadStep (node_modules\dropbox\lib\dropbox.js:1454:19)
  .....
当我尝试运行代码时出现

错误。如果我给出错误的'作为光标,误差不会在步进函数中发生,但在完成函数中仍然会发生。任何人都可以帮我吗?

stream.on('data', function (data) {
    client.resumableUploadStep(data, function (error, cursor) {
       if (error) {
          return  console.log(error);
       }    
    })
});

stream.on('end', function () {
     client.resumableUploadFinish(fileName, function (error, stats) {
         if (error) {
             return callback(error);
         }
         return callback(null, stats);
     });
});

我使用了以下代码,现在可以使用了。

var pcursor = null;
var eventObject = new EventEmitter();
var counter = 0;
stream.on('data', function (data) {
    counter++;       
    client.resumableUploadStep(data, pcursor, function (error, cursor) {
            if (error) {                   
                return  callback(error);
            }
            counter--;
            pcursor = cursor;
            eventObject.emit('event');               
     });
});

stream.on('end', function () {
        eventObject.on('event', function () {
            if (counter == 0) {
                client.resumableUploadFinish(fileName, pcursor, function (error, stats) {
                    if (error) {
                        return callback(error);
                    }
                    return callback(null, stats);
                });
                counter = -1;
            }
        });

        eventObject.emit('event');
 });

1 个答案:

答案 0 :(得分:1)

您似乎错过了resumableUploadFinish的游标参数。此外,您应该在第一次调用后将光标传递给resumableUploadStep

我认为您想要的代码是这样的(完全未经测试):

var cursor = null;
stream.on('data', function (data) {
    client.resumableUploadStep(data, cursor, function (error, new_cursor) {
        cursor = new_cursor;
    });
});
stream.on('end', function () {
    client.resumableUploadFinish(fileName, cursor, function (error, stats) {
        return callback(null, stats);
    });
});