我正在使用Node.js中的mssql api连接到SQL数据库, 我从配置json文件获取连接信息:
"mssql": {
"user": "XX",
"password": "XXXX",
"server": "XXXXXX",
"database": "XXXXXX"
},
配置读得很好,我使用这些功能进行连接和查询:
var olderOneDayMSSQL = (query) => {
sql.connect(dbconfig).then(async () => {
const result = await sql.query(query);
//console.log(result);
sql.close();
return result;
}).catch((err) =>{
console.log(err);
sql.close();
}).finally(()=> {
sql.close();
});
}
var test3 = (req,res,query) => {
var conn = new sql.ConnectionPool(dbconfig);
var req = new sql.Request(conn);
var result;
return conn.connect().then(async() => {
result = await req.query(query);
//console.log(result);
conn.close();
return result;
//console.log(result);
//conn.close();
}).catch(e => {
return e;
}).finally(() => {
conn.close();
});
}
在我的路线中,我正在调用这些函数:
router.get("/countMSSQL1", function(req,res) {
var query = `SELECT TOP 1 title,id
FROM [XX].[dbo].[XXX] where lastChange < dateadd(day,-1,getdate())`;
olderOneDayMSSQL(query).then((result) => {
res.json(result);
}).catch((err) => {
res.status(500).send({ message: `${err}`});
});
});
router.get("/testQuery", async function(req,res) {
var query = `SELECT Top 3 title
FROM [XXX].[dbo].[XXXX] where lastChange < dateadd(day,-1,getdate())`;
var result1 = await test3(query);
res.json(result1);
}
但是我收到此错误:
TypeError: Cannot add property port, object is not extensible
at new ConnectionPool (C:\Users\m\Desktop\index-watcher-m\node_modules\mssql\lib\base.js:141:22)
at new ConnectionPool (C:\Users\m\Desktop\index-watcher-m\node_modules\mssql\lib\tedious.js:193:1)
at Object.connect (C:\Users\m\Desktop\index-watcher-m\node_modules\mssql\lib\base.js:1637:22)
奇怪的是,自昨天以来我没有收到此错误。.?
编辑:我在这里了解到Object.preventExtension() https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Fehler/Cant_define_property_object_not_extensible 但我从未设置过该属性...
编辑:如果我手动输入常规json配置对象作为sql.conn(Parameter) 正常工作,为什么会出现该错误?没有权限?