我试图将数据插入我的Azure数据库然后阅读它。我创建了一个单独的数据配置和全局连接,然后我可以从应用程序中的任何位置访问它。服务器已连接,但是,当我在路由中写入插入函数时,我遇到了这个错误:db.Request不是路由index.js中的构造函数。这是我的代码:
dbconfig.js:
const sql = require('mssql');
const config = {
user: 'admin',
password: 'admin',
server: 'admin.database.windows.net',
database: 'em-cryptolend-db',
// If you are on Microsoft Azure, you need this:
options: {encrypt: true}
}
const connection = sql.connect(config, err => {
if(err) throw err;
else console.log("SQL Server connected");
})
module.exports = connection;
app.js:
const http = require('http');
const express = require('express');
const exphbs = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const sql = require('mssql');
const request = require('request');
// Express app setup
const app = express();
// BodyParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Express Validator
app.use(expressValidator());
// Routes
const routes = require('./routes/index');
const users = require('./routes/users');
app.use('/', routes);
app.use('/users', users);
// View engines
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout:'layout'}));
app.set('view engine','handlebars');
// Static folder
app.use(express.static(path.join(__dirname, 'public')));
// Listen to the server
const port = process.env.PORT || 1337;
const server = app.listen(port, function(){
console.log("Server started on port " + port);
});
路由/ index.js:
const express = require('express');
const router = express.Router();
const db = require('../dbconfig');
// Get homepage
router.get('/', function(req,res) {
var request = new db.Request();
request.query("INSERT INTO dbo.lendbook (id, rate, amount, period, timestamp, type, frr) VALUES (8, 0.6, 5, 4, 2009-11-11 13:23:44.000, 'eth', 'bbb' )").then(function(recordset) {
console.log('Recordset: ' + recordset);
console.log('Affected: ' + request.rowsAffected);
}).catch(function(err) {
console.log('Request error: ' + err);
});
});
module.exports = router;
版本: 节点:6.9.1 mssql:4.0.4
提前谢谢。
答案 0 :(得分:1)
请更改您的代码,如下所示:
dbconfig.js
const sql = require('mssql');
const config = {
user: 'admin',
password: 'admin',
server: 'admin.database.windows.net',
database: 'em-cryptolend-db',
// If you are on Microsoft Azure, you need this:
options: {encrypt: true}
}
module.exports = config;
路由/ index.js
const express = require('express');
const router = express.Router();
const config = require('../dbconfig');
const sql = require('mssql');
// Get homepage
router.get('/', function(req, res) {
sql.connect(config).then(function(err) {
var request = new sql.Request().query("INSERT INTO dbo.lendbook (id, rate, amount, period, timestamp, type, frr) VALUES (8, 0.6, 5, 4, 2009-11-11 13:23:44.000, 'eth', 'bbb' )").then(function(recordset) {
console.log('Recordset: ' + recordset);
console.log('Affected: ' + recordset.rowsAffected);
res.status(200).send('Inserted successfully.');
}).catch(function(err) {
console.log('Request error: ' + err);
res.status(500).send('Request error: ' + err);
});
})
});
module.exports = router;