【问题标题】:SailsJS/NodeJS: Calling method from Controller with PromiseSailsJS/NodeJS:使用 Promise 从控制器调用方法
【发布时间】:2019-09-18 11:53:14
【问题描述】:

我正在尝试从我的 Controller 调用服务方法,但出现以下错误:

错误:

TestService.importDataFromFile(file).then( (响应) => {

TypeError: 无法读取未定义的属性 'then'

控制器

massImportData(req, res) {

        try {

            TestService.importDataFromFile(file).then( (response) => {

                // todo

            });
    } catch(err) {
        // ...
    }

}

服务

    importData(data) {

        try {

            importDataFromFile(data, { schema, sheet: "misc" }).then(function({ rows, errors }) {

                CarModel.createEach(rows).then(function (user){
                    return {
                        status  : "OK"
                    }
                })

            }).catch(err => {
                // ...
            });

        } catch (err) {
            // ...
        }

   }

【问题讨论】:

    标签: javascript node.js promise sails.js


    【解决方案1】:

    假设您使用的是原生承诺,请尝试以下操作,

    console.log(importDataFromFile(data, { schema, sheet: "misc" }) instanceof Promise);
    

    由于某些原因,您的 importDataFromFile() 函数似乎没有返回承诺

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多