gpt4 book ai didi

javascript - Sequelize sqlite3连接到数据库但在获取数据时抛出错误

转载 作者:行者123 更新时间:2023-12-03 01:38:33 25 4
gpt4 key购买 nike

我在nodejs中使用sequelize和sqlite3。

与数据库文件的连接运行良好,没有错误,但是当第一次尝试访问它时,我收到以下错误(无论我尝试执行什么表或查询):

TypeError: Cannot read property 'findOne' of undefined
at l.a.sequelize.authenticate.then (/usr/local/bin/aeirtu/node/webpack:/server.js:55:1)
at tryCatcher (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/usr/local/bin/aeirtu/node/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:763:18)
at tryOnImmediate (timers.js:734:5)
at processImmediate (timers.js:716:5)

这是我的连接代码:

我的node.js代码:

import express from "express";

import models from "./models";

const app = express();

models.sequelize
.authenticate()
.then(() => {
console.log("Connected to repository");
})
.catch(err => {
console.log("ERROR connecting to repository.");
console.log(err);
});

let port = 3001;
app.listen(port, () => {
console.log("Server listening on port " + port);
});

我的 models.js:

import Sequelize from "sequelize";
import fs from "fs";
import path from "path";

const DATABASE_FILENAME = "test.db";

const fileWithPath = path.join(process.env.TEST_HOME, 'data', DATABASE_FILENAME);

let basename = path.basename(__filename);
let env = process.env.NODE_ENV || "development";
let db = {};

console.log("Connecting to repository at " + fileWithPath);

// database wide options
let opts = {
dialect: "sqlite",
storage: fileWithPath,
define: {
//prevent sequelize from pluralizing table names
freezeTableName: true,
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false
},
operatorsAliases: Sequelize.Op, // use Sequelize.Op https://github.com/sequelize/sequelize/issues/8417#issuecomment-334056048
};

let sequelize = new Sequelize(DATABASE_FILENAME, null, null, opts);

fs
.readdirSync(__dirname)
.filter(file => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach(file => {
let model = sequelize["import"](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

export default db;

还有一个简单的测试代码:

import db from "./models";

db.TestTable.findOne().then(data => {
if (!data) console.log("ERROR FETCHING DATA");
console.log(data.name);
});

我已经三重检查:sqlite3 文件位于正确的位置,并且表格中包含数据。由于某种原因我当时无法访问。我还在数据库文件中更改了 chmod a+rwx。

最佳答案

您忘记导入 *TestTable 模型`

db.TestTable = require('./TestTable')(sequelize, Sequelize);

你的model.js应该是一个连接但还没有定义模型,例如

module.exports = (sequelize, DataTypes) => {  
const testTable = sequelize.define('model', {
states: {
type: Sequelize.ENUM,
values: ['active', 'pending', 'deleted']
}
});

return testTable;
};

Sample

关于javascript - Sequelize sqlite3连接到数据库但在获取数据时抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50959776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com