回调不是sqlite中的函数

问题描述 投票:0回答:1

我试图让class DATABASE创建db.run到可重用的方法,通过回调调用SQLite中的创建更新和删除,这是我的代码


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语句,callback({this.lastID})当我评论并删除else语句时,它成功创建了表,

但是如果我再次编写那个else语句,则错误消息是CALLBACK IS NOT FUNCTION但是create table成功了

意思是this.lastId是sqlite3给出这个信息的方式是它在运行的上下文(SQL,[params],function(err){this.lastID})回调函数中的'this'对象上放置一个字段。但是,仅在使用INSERT语句调用run时。否则,它只是0本教程说https://stackabuse.com/a-sqlite-tutorial-with-node-js/

你认为我的回调是不正确的?

node.js oop sqlite callback
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.