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