如何访问嵌套的承诺数据?

时间:2020-03-19 15:45:46

标签: node.js express promise sftp

我正在尝试建立一条路由,以将数据从嵌套的Prom发送到我的Vue应用。

但是我在从嵌套承诺中获取数据时遇到了麻烦。

我尝试使用没有成功的回调

app.get('/notification', (req, res) => {
  const getData = (data) => {
    console.log(data)
  }
  scheduler(data)
})



const scheduler = (callback) => {

      sftp
        .connect({ credentials })
        .then(() => {
          return sftp.list(root);
        })
        .then(async data => 
          {
          const filteredFile = data.filter(file => {
            let currentDate = moment();
            let CurrentTimeMinusFive = moment().subtract(5, "minutes");
            let allAccessTimes = file.accessTime;
            let parsedAccessTimes = moment(allAccessTimes);
            let filteredTime = moment(parsedAccessTimes).isBetween(
              CurrentTimeMinusFive,
              currentDate
            );
            return filteredTime;
          });
          for (const file of filteredFile) {
            let name = file.name;
            let filteredThing;
            await sftp
              .get(`Inbound/${name}`)
              .then(data => {
                csv()
                  .fromString(data.toString())
                  .subscribe(function (jsonObj) {
                    return new Promise(function (resolve, reject) {
                      filteredThing = new Notification(jsonObj);
                      filteredThing.save()
                      .then(result => {
                        console.log(result);
                        callback(result) **// THIS IS THE RESULT I NEED IN MY FRONT END**
                      })
                      .catch(err => {
                        console.log(err);
                      });
                      resolve();
                    });
                  });
              });
          }
        })

当我进入本地主机/通知时,我得到:

ReferenceError:未定义数据

谢谢!

0 个答案:

没有答案