回调不是sqlite中的函数

时间:2019-03-17 10:23:56

标签: node.js oop sqlite callback

我试图使class DATABASE为可重用方法创建db.run,以通过回调在SQLite中调用create update和delete, 这是我的代码


const db = new sqlite3.Database("database.db", err => {
  if (err) {
    console.log(`can;t connected tp database, status: ${err}`);
  } else {
    console.log(`gotcha, succesfully connect to the database`);
  }
});

class DATABASE {
  static run(sql, params = [], callback) {
    db.run(sql, params, function(err) {
      if (err) {
        throw err;
        console.log(`something wrong with this ${sql}`);
        console.log(err);
        callback(err);
      } else {
        callback({"id" : this.lastID}) // this is could be null also
        console.log("sucesfully fo this ", sql);
      }
    });
  }
}

module.exports = DATABASE;

然后我通过类似这样的方式来调用该类:

const DATABASE = require("./setup.js");
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("database.db", err => {
  if (err) {
    console.log(`can;t connected tp database, status: ${err}`);
  } else {
    console.log(`gotcha, succesfully connect to the database`);
  }
});

db.serialize(() => {
  const departement = `CREATE TABLE IF NOT EXISTS departement(
                      id INTEGER PRIMARY KEY AUTOINCREMENT,
                      name TEXT NOT NULL,
                      city TEXT
                      )`;

  const employee = `CREATE TABLE IF NOT EXISTS employee(
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    firstName TEXT NOT NULL,
                    lastName TEXT,
                    gender TEXT NOT NULL CHECK (gender IN ('Male', 'Female')),
                    email TEXT NOT NULL UNIQUE ,
                    phone TEXT,
                    dept_id INTEGER,
                    FOREIGN KEY (dept_id) REFERENCES departement(id)
                    )`;
  DATABASE.run(departement);
  DATABASE.run(employee);

  // , err => {
  //   if (err) throw new Erro(`bugs here`);
  //   else console.log("sucess.....");
  // });
});

当我注释并删除else语句时,您可以看到else语句callback({this.lastID})

但是如果我再次写else语句,则错误消息是CALLBACK IS NOT FUNCTION 但是创建表成功

this.lastId的含义是 sqlite3给出此信息的方式是将它在run(SQL,[params],function(err){ })回调函数...但是,仅在使用INSERT语句调用run的情况下。否则,如本教程所述https://stackabuse.com/a-sqlite-tutorial-with-node-js/

,它的值为0

您认为我的回叫不正确吗?

1 个答案:

答案 0 :(得分:0)

问题出在您编写callback({this.lastID})的方式上,将其更改为callback(this.lastID)callback({"lastID" : this.lastID})

`const DATABASE = require("./setup");
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("database.db", err => {
  if (err) {
    console.log(`can;t connected tp database, status: ${err}`);
  } else {
    console.log(`gotcha, succesfully connect to the database`);
  }
});
var abc= function(arg){
  console.log(arg);  
};

db.serialize(() => {
  const departement = `CREATE TABLE IF NOT EXISTS departement(
                      id INTEGER PRIMARY KEY AUTOINCREMENT,
                      name TEXT NOT NULL,
                      city TEXT
                      )`;

  const employee = `CREATE TABLE IF NOT EXISTS employee(
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    firstName TEXT NOT NULL,
                    lastName TEXT,
                    gender TEXT NOT NULL CHECK (gender IN ('Male', 'Female')),
                    email TEXT NOT NULL UNIQUE ,
                    phone TEXT,
                    dept_id INTEGER,
                    FOREIGN KEY (dept_id) REFERENCES departement(id)
                    )`;
  DATABASE.run(departement, abc);
  DATABASE.run(employee, abc);

  // , err => {
  //   if (err) throw new Erro(`bugs here`);
  //   else console.log("sucess.....");
  // });
});`

然后您运行setup.js

const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("database.db", err => {
  if (err) {
    console.log(`can;t connected tp database, status: ${err}`);
  } else {
    console.log(`gotcha, succesfully connect to the database`);
  }
});

class DATABASE {
  static run(sql, callback) {
    db.run(sql, function(err) {
      if (err) {
        throw err;
        console.log(`something wrong with this ${sql}`);
        console.log(err);
        callback(err);
      } else {
        callback(this.lastID) // this is could be null also
        console.log("sucesfully fo this ", sql);
      }
    });
  }
}

module.exports = DATABASE;