Node.js 如何在Express.js中将数据库查询从模型传递到控制器
我有一个模型文件Node.js 如何在Express.js中将数据库查询从模型传递到控制器,node.js,express,Node.js,Express,我有一个模型文件product.js,它执行以下操作: const mssql = require('mssql/msnodesqlv8'); module.exports.rsProducts = function () { // update1: removed params req,res,next global.MYDB.connect(function (error) { // MYDB is a mssql-server database setup in app
product.js
,它执行以下操作:
const mssql = require('mssql/msnodesqlv8');
module.exports.rsProducts = function () { // update1: removed params req,res,next
global.MYDB.connect(function (error) { // MYDB is a mssql-server database setup in app.js as a global variable
if (error) {
console.log(error);
return;
}
global.MYDB.request().query('SELECT Top(10) * FROM [dbo].[Product]', function (err, result) {
console.log(result); // this works and i see the database recordset in the console
return(result); // this does not seem to do anything (see below)
});
});
我有一个名为index.js
的控制器文件,它像这样调用模型:
var router = require('express').Router();
const mssql = require('mssql/msnodesqlv8');
const Offer = require('../models/product');
Offer.rsProducts(); // the console.log(result) line in the model file logs the recordset to the console
console.log(Offer.rsProducts()); // this log does not log anything which means no result is being returned from the model file to this controller
我的问题是为什么product.js
模型中的返回(结果)
行不向我的控制器返回任何内容?最终,我希望控制器呈现如下视图文件:
var router = require('express').Router();
const mssql = require('mssql/msnodesqlv8');
const Offer = require('../models/product');
Offer.rsProducts(); // the console.log(result) line in the model file logs the recordset to the console
console.log(Offer.rsProducts()); // this log does not log anything which means no result is being returned from the model file to this controller
router.get('/',函数(req,res,next){
res.render('index',{pagetitle:'Product Name',数据:Offer.rsProducts()});
});代码>
上述方法从来都不起作用,因为Offers.rsProduct()
没有任何结果
更新1
我需要一个可伸缩的答案,以便控制器可以根据需要调用任意多个模型中的任意多个数据库函数。该网站的主页需要来自大约4种不同模型的数据库数据。实际上,最终的结果(我在脑海中想象)是控制器将执行以下操作(伪代码):
有太多的事情需要解释
如您所见,所有“I/O”函数(如连接到数据库、查询到数据库…)都有一个参数,如function(err,result){…
(function(error)
…),以及您的问题“为什么它们不直接返回结果而不是将其包装到函数中?”
对于Javascript,该函数是一个回调
函数,JS的工作方式与“普通”程序语言不同
对于您的情况,我不知道您为什么使用req、res、next
作为参数定义rsProduct
函数,但这对我的方式很好-使rsProduct
成为一个“”函数:
const mssql = require('mssql/msnodesqlv8');
module.exports.rsProduct = function (req, res, next) {
global.MYDB.connect(function (error) { // MYDB is a mssql-server database setup in app.js as a global variable
if (error) {
console.log(error);
return next(error); // call next with a error to stop flow and throw a error to express app
}
global.MYDB.request().query('SELECT Top(1) * FROM [dbo].[Prodcut]', function (err, result) {
console.log(result); // this works and i see the database recordset in the console
// !!! Assign the result to a new field of req object
req.products = result; // maybe what you need is req.products = result.recordset;
next(); // continue to your function to render view
});
})
}
然后在你的路由器里
var router = require('express').Router();
const mssql = require('mssql/msnodesqlv8');
const Offer = require('../models/product');
// Offer.rsProduct(); // the console.log(result) line in the model file logs the recordset to the console
// console.log(Offer.rsProduct()); // this log does not log anything which means no result is being returned from the model file to this controller
router.get('/', Offer.rsProduct, function (req, res, next) { // Offer.rsProduct instead of Offer.rsProduct()
res.render('index', { pagetitle: 'Product Name', data: req.products }); // get back products list
});
我认为您没有理解模型的要点。模型应该告诉您的数据库您的数据是如何构造的。其次,您实际上没有从rsProduct返回任何内容。这里不需要,但您没有设置Offer.rsProduct()
任何这样的return
都无处可返回。我不确定,但JavaScript中的return
可能不是最好的词…它是一个保留词。请尝试const result=Offer.rsProduct();console.log(result)
@denikov javascript如何在不使用return
函数的情况下将数据返回到调用文件?你说得对,我误解了。我以为return是你的回调参数之一。我认为这不太管用。我已经更新了我的问题Update 1
,这样你就可以更好地了解我是如何看待这个问题的否则我会被困在如何从数据库中获取主页数据的问题上。