在Node JS中调用时,JavaScript函数返回未定义的

时间:2019-10-01 14:58:22

标签: javascript node.js

我为对SQL Server数据库的NodeJS查询创建了一个存储库,并创建了在返回未定义时返回数组的函数。但是当我console.log(personList)它返回数据

const sql = require('mssql');
//Application = require('../Models/application');;

class ApplicationRepo {

getApplication(){
    console.log('---db conn----');
    var personList = [];
    const request = new sql.Request();
    request.query("SELECT * FROM Students", function(err, rows, fields) {
      if (err) {
        res.status(500).json({"status_code": 500,"status_message": "internal server error"});
      } else {
        console.log(rows.recordsets[0].length);
        var lng = rows.recordsets[0].length;
        var rc = rows.recordsets[0];
        for (var i = 0; i < lng; i++) {
          var person = {
            'LastName':rc[i].LastName,
            'FirstName':rc[i].FirstName
          }
          personList.push(person);
        }
        //console.log(personList);
        return personList;
     }
    })
 }
 }

 module.exports = new ApplicationRepo;

在我的index.js中,我将其称为getApplication()函数返回未定义

 var express = require('express');
 const sql = require('mssql');
 var router = express.Router();
 const appRepo = require('../lib/applicationRepo');

 router.get('/', function(req, res, next) {
   console.log(appRepo.getApplication());
   //res.render('index', {"myres": personList});
  res.send('Hello');

 });
 module.exports = router;

0 个答案:

没有答案