node.js - Sequelize - NodeJS - 返回 o 函数未定义

标签 node.js sql-server sequelize.js

我试图了解如何修复我的代码,因为我的函数返回未定义。我正在用 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);

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

谢谢。

最佳答案

函数 getCreditsStudent 实际上不返回任何内容。相反,它只是定义了一个异步进程。您需要返回您定义的 promise ,然后使用 async/await 或 promise 链来使用结果。

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...
});

关于node.js - Sequelize - NodeJS - 返回 o 函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214172/

相关文章:

SQL Server 将列显示为根据可重复键分隔

node.js - npm start 上的 Sequelize 错误

node.js - 使用 DirectLine JS 在 NodeJs 中自定义 Azure 机器人框架

javascript - 在 sails.js 下使用 SSL 时 Passport.js 无法初始化

node.js - Telegraf 和 Heroku 未在 webhook [nodejs] 上发送第一个回复

sql-server - FREETEXTTABLE 为什么或如何给出比其他人更高的排名值

javascript - 如何删除 Mongoose 文档并从另一个模型引用它?

sql - (SQL) 从大列表中提取特定数据,其中某些日期属于特定季度

node.js - 使用 sequelize 时如何解决 SequelizeDatabaseError NaN?

postgresql - Sequelize Many to Many Include 失败,列不存在