如何解决“ TypeError:this.parent.acquire不是函数”

时间:2019-06-12 22:28:53

标签: javascript node.js sql-server npm

我正在使用node.js设置服务器以在数据库中插入数据。我不断收到相同的错误。我什至找不到我的代码中的acquire函数,尝试在github上寻找mssql,但仍然一无所获。理想情况下,它应该在我的数据库中插入一个值。

const express = require('express');
const cors = require('cors')
const mssql = require('mssql');

const app = express();

app.use(cors());

const config = {
    user: "admin",
    password: "admin",
    server: "DESKTOP-0NPPB7V",
    database: "Lab_Db"
};

console.log(config);



app.get('/', (req, res) => {
    res.send('hey')
});

app.get('/add-worker', (req, res) => {

    const {Name, sName, workTime} = req.query;
    const INSERT_QUERY = `INSERT INTO personel(FirstName, SecondName, WorkTime) VALUES ('${Name}', '${sName}', '${workTime}')`;
    console.log(INSERT_QUERY)
    mssql.close();
    mssql.connect(config, function (err) {
        console.log('connected1')

        if (err) console.log(err);

        const transaction = new mssql.Transaction(/ * [pool] */)
        console.log('connected2')
        transaction.begin(function(err) {
            console.log('connected3')
            if (err) console.log(err);

            var request = new mssql.Request();
            console.log('connected')

            request.query(INSERT_QUERY, function (err, result) {
                console.log('connected')

                if (err) console.log(err)

                transaction.commit(function(err) {
                    console.log('connected')
                    if (err) console.log(err)
                    else
                        return res.send('successfully added worker')

                });
            });
        });
    });
});



app.listen(4000, () => {
console.log('Listening on port 4000')
});

所以我希望“成功添加工作人员”,但我却得到:

...
connected1
connected2
C:\Users\...\Desktop\кс\lab\node_modules\mssql\lib\tedious.js:330
      this.parent.acquire(this, (err, connection, config) => {
                  ^

TypeError: this.parent.acquire is not a function
    at Immediate._begin.err (C:\Users\...\Desktop\кс\lab\node_modules\mssql\lib\tedious.js:330:19)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)

我对node.js或javascript不太熟悉,甚至无法想象我需要对此进行故障排除。

0 个答案:

没有答案