vue2使用better sqlite3为什么会报错?

vue2使用better sqlite3打开页面就报错
版本:
better-sqlite3 8.3.0
nodejs 16.20.2
在src/api下创建了database.js代码:

DB.db是我用SQLiteStudio在D盘手动创建的db文件

const Database = require("better-sqlite3")
const path = require('path')
import { getCurrentTime } from '@/utils/index'

const dbPath = path.resolve('D:/DB.db')
console.log(dbPath)
const db = new Database(dbPath, { verbose: console.log })

export function addUser() {
  var stmt = db.prepare("insert into user(id,name, describe,update_date,create_date) values(?,?,?,?,?)");
  stmt.run(null,'测试1','测完给i', getCurrentTime(), getCurrentTime());
  var return_info = {
    'result': 1,
    'info': '新增用户成功'
  }
  return return_info
}

页面调用:

<template>
  <div>
    <el-button type="primary" icon="el-icon-edit" @click="add">Add</el-button>
  </div>
</template>

<script>
import { addUser } from '@/api/database.js'
export default {
    name: 'test',
    methods: {
        add(){
          addUser.then(res=>{
            console.log(res)
          })
        }
    }
}
</script>

报错信息:

TypeError: The "original" argument must be of type Function
    at promisify (util.js:602:11)
    at eval (backup.js:6:18)
    at ./node_modules/better-sqlite3/lib/methods/backup.js (chunk-vendors.js:1697:1)
    at __webpack_require__ (app.js:849:30)
    at fn (app.js:151:20)
    at Object.eval (database.js:72:29)
    at eval (database.js:86:30)
    at ./node_modules/better-sqlite3/lib/database.js (chunk-vendors.js:1661:1)
    at __webpack_require__ (app.js:849:30)
    at fn (app.js:151:20)

更换了better sqlite3版本还是一样

阅读 2.2k
4 个回答

没用过这个库。看了介绍这是一个运行在Node.js中访问和操作SQLite3的库,不能直接在客户端使用。

前端数据库技术有限,大概只有这么些

实现技术有效时长存储内容规模
cookie页面关闭前字符串K
sessionStorage页面关闭前字符串M
localStorage永久字符串M
indexedDB永久多种类型100M

可以看出来是不支持 sqlite 的,所以基于 sqlite 的库,应该都只能在 Node 环境下运行。在浏览器环境下,需要大量存储数据可以使用 indexedDB(IndexedDB | MDN),页面上还介绍了几个对 IndexedDB 进行高级封装的 JS 库。微软也介绍了基于浏览器的用法(查看和更改 IndexedDB 数据 | Microsoft Learn)。

better-sqlite3 
The fastest and simplest library for SQLite3 in Node.js.

也就是说这个库只能在nodejs环境中运行!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题