gpt4 book ai didi

node.js - 如何将数据库查询从模型传递到 Express.js 中的 Controller

转载 作者:行者123 更新时间:2023-12-03 22:14:30 25 4
gpt4 key购买 nike

我有一个模型文件 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 的 Controller 文件,它像这样调用模型:

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 模型中的 return(result) 行没有向我的 Controller 返回任何内容?最终我希望 Controller 呈现这样的 View 文件:

router.get('/', function(req, res, next) {
res.render('index', { pagetitle: 'Product Name', data: Offer.rsProducts() });
});

上面的代码永远行不通,因为 Offers.rsProduct() 没有返回任何内容。

更新 1我需要一个可扩展的答案,以便 Controller 可以根据需要从尽可能多的模型中调用尽可能多的数据库函数。该网站的主页需要来自大约 4 个不同模型的数据库数据。真的,最终结果(我在脑海中想象)是 Controller 会做这样的事情(伪代码):

var router =  require('express').Router();
const Offer = require('../models/product');
const Story = require('../models/story');
const QandA = require('../models/qanda');
const Job = require('../models/job');

router.get('/', function(req, res, next) {
res.render('index', {
pageTitle: 'Homepage',
offerData: Offer.rsProducts(), //calls Offer's rsProducts function to get the data
storyData: Story.rsStories(), // calls Story's rsStories function to get the data
qandaData: QandA.rsQandAs(), // calls QandA's rsQandAs function to get the data
jobData: Job.rsJobs() // calls Job's rsJobs function to get the data
});
});

最佳答案

这里需要解释的东西太多了。

如您所见,所有“I/O”函数(如连接数据库、查询数据库...)都有一个类似function (err, result) {... (function (error)...),以及您的问题“为什么他们不直接返回结果,而是将其包装到一个函数中?”。

对于 Javascript,该函数是一个Callback 函数,JS 不像“普通”程序语言那样工作。

针对您的情况的修补程序,我不知道您为什么使用 req, res, next 作为参数定义 rsProduct 函数,但这对我来说很有用 -使 rsProduct 成为一个“Middleware”函数:

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

关于node.js - 如何将数据库查询从模型传递到 Express.js 中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com