【问题标题】:Sequelize - NodeJS - Return o function is undefinedSequelize - NodeJS - 返回函数未定义
【发布时间】:2018-11-09 03:26:59
【问题描述】:

我正在尝试了解如何修复我的代码,因为我的函数返回未定义。我正在使用 Sequelize 和 NodeJS 编写代码。

exports.getCreditsStudent = function(sgecode,collections,grade,year){
mssql.query('SELECT total FROM [dbo].[creditosAvulsos] WHERE codsge = $sgecode1 and nivelensino_idnivelensino = $collections1 and produto_idproduto = $grade1 and ano = $year1',
{ bind:{sgecode1: sgecode, collections1: collections, grade1: grade, year1: year}, type: mssql.QueryTypes.SELECT})
.then(total => {
    return total
  })

}

我在我的 Service.js 上调用这个函数,像这样:

examples = db.getCreditsStudent(sgecode,collections,grade,year);

问题出在哪里以及如何解决?

谢谢。

【问题讨论】:

    标签: node.js sql-server sequelize.js


    【解决方案1】:

    函数getCreditsStudent 实际上并没有返回任何东西。相反,它只是定义了一个异步进程。您需要返回您定义的承诺,然后使用 async/await 或承诺链来使用结果。

    exports.getCreditsStudent = function(sgecode,collections,grade,year){
      return mssql.query('SELECT total FROM [dbo].[creditosAvulsos] WHERE codsge = $sgecode1 and 
      nivelensino_idnivelensino = $collections1 and produto_idproduto = $grade1 and ano =  $year1',
      { bind:{sgecode1: sgecode, collections1: collections, grade1: grade, year1: year},
      type: mssql.QueryTypes.SELECT})
      .then(total => {
          return total
        })
    }
    

    然后你可以在异步函数中做

    examples = await db.getCreditsStudent(sgecode,collections,grade,year);
    

    或者你可以使用

    db.getCreditsStudent(sgecode, collections, grade, year)
    .then(value => {
       examples = value;
       // whatever you want to do with examples...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多