node.js - How to solve reference error response data is not defined in MEAN stack api project? -
i’m trying build api mean stack, when test endpoint request using postman, following error:
referenceerror: studentdata not defined @ module.exports.studentsgetall (f:\mean\api\controllers\students.controllers.js:13:11) @ layer.handle [as handle_request] (f:\mean\node_modules\express\lib\router\layer.js:95:5) @ next (f:\mean\node_modules\express\lib\router\route.js:137:13) @ route.dispatch (f:\mean\node_modules\express\lib\router\route.js:112:3) @ layer.handle [as handle_request] (f:\mean\node_modules\express\lib\router\layer.js:95:5) @ f:\mean\node_modules\express\lib\router\index.js:281:22 @ function.process_params (f:\mean\node_modules\express\lib\router\index.js:335:12) @ next (f:\mean\node_modules\express\lib\router\index.js:275:10) @ function.handle (f:\mean\node_modules\express\lib\router\index.js:174:3) @ router (f:\mean\node_modules\express\lib\router\index.js:47:12)
however if send second request studentdata response returned want it. doing wrong?
students.controllers.js
var dbconn = require('../data/dbconnection.js'); module.exports.studentsgetall = function( req, res) { dbconn.open(); console.log('get students'); res .status(200) .json(studentdata); };
dbconnection.js
var sql = require('mssql'); var query = require('./queries.js'); var dburl = 'mssql://*****:****@**.**.*.**/******/*******'; function open() { var conn = new sql.connection(dburl); var req = new sql.request(conn); conn.connect(function (err) { if (err) { console.log(err); return; } console.log("database connection established"); req.query(query.getallquery, function (err, recordset) { if (err) { console.log(err); } else { studentdata = recordset; } }); }); } module.exports = { open : open };
how can define response data before exists? confused can explain i'm going wrong please?
during first try, code respond before database connection established. second time work better because db connection established after first try.
a database connection block i/o. meaning should implement callback function performed after connection made , respond data retrieved.
i re-write students.controllers following:
// students.controllers.js var dbconn = require('../data/dbconnection.js'); module.exports.studentsgetall = function( req, res) { var getstudentdata = function(studentdata){ res .status(200) .json(studentdata); }; dbconn.open(getstudentdata); console.log('getting students records'); } //dbconnection.js var sql = require('mssql'); var query = require('./queries.js'); var dburl = 'mssql://*****:****@**.**.*.**/******/*******'; function open(callback) { var conn = new sql.connection(dburl); var req = new sql.request(conn); conn.connect(function (err) { if (err) { console.log(err); callback({error: err}); // respond error return; } console.log("database connection established"); req.query(query.getallquery, function (err, recordset) { if (err) { console.log(err); callback({error: err}); // respond error } else { // added callback callback(recordset); // respond data set } }); }); } module.exports = { open : open };
Comments
Post a Comment