handel 从 dropbox-api 返回对象(node js 和 express)

handel returned object from dropbix-api (nodejs and express)

我正在尝试执行一个简单的操作,例如将文件上传到保管箱, 文件上传成功 我需要的是包含文件名、大小、路径等的 returned 答案

我知道我在异步调用中迷路了, 我想在这里得到一些帮助:

exports.uploadFile =  async function () {
    fs.readFile('./text.txt',  function (err, contents) {
                if (err) {
                     console.log('Error: ', err);
                }
                    uploadFile(contents);
            });
          } ;
async function  uploadFile(fileCont) {
         let dbx =  new Dropbox({ accessToken: APP_KEY });
         await dbx.filesUpload({ path: '/basic4.txt', contents: fileCont })
         .then(function (response) {
           console.log( response);
           return response;
         })
        .catch(function (err) {
             console.log(err);
         });
}

并且我想 return 返回结果,所以我使用了这部分:

DriveService.uploadFile()
    .then((success)=>{
        return res.status(200).json({success:true,data:success,message:'list of files recived'});
})
.catch((error)=>{
    return res.status(400).json({success:false,data:{},message:error.message});
})

问题是成功总是空的,因为我在异步森林中迷路了。

有人可以指教吗?

谢谢

不确定异步解决方案但是,您可以像这样使用回调:

exports.uploadFile =  async function (cb) {
    fs.readFile('./text.txt',  function (err, contents) {
                if (err) {
                     console.log('Error: ', err);
                }
                    uploadFile(contents,cb);
            });
          } ;


async function  uploadFile(fileCont,cb) {
         let dbx =  new Dropbox({ accessToken: APP_KEY });
         await dbx.filesUpload({ path: '/basic4.txt', contents: fileCont })
         .then(function (response) {
           console.log( response);
           cb(response);//Pass response in callback
         })
        .catch(function (err) {
             console.log(err);
         });
}
DriveService.uploadFile(function(success) {//this callback will be called from async 
 return res.status(200).json({success:true,data:success,message:'list of files recived')
})
.catch((error)=>{
    return res.status(400).json({success:false,data:{},message:error.message});
})