我是新使用adminjs。我创建了一个 Node Express js 项目来处理我的后端。我使用 postgresql 作为数据库。这样我使用
@adminjs/sql
管理员身份验证就可以正常工作。但是我在尝试显示 postgresql 的表时遇到了一些问题。由于我是初学者,我遵循了官方文档https://docs.adminjs.co/installation/adapters/sql
这是我的 app.js
文件:
const express = require("express");
require("express-async-errors");
const cors = require("cors");
const morgan = require("morgan");
const routes = require("./routes");
const compression = require("compression");
const unknownEndpoint = require("./middleware/unKnownEndpoint");
const { handleError } = require("./helpers/error");
const app = express();
app.set("trust proxy", 1);
app.use(cors({ credentials: true, origin: true }));
app.use(express.json());
app.use(morgan("dev"));
app.use(compression());
app.use("/uploads", express.static("uploads"));
// CORS handling
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).json({});
}
next();
});
app.use(`/api`, routes);
Promise.all([
import('adminjs'),
import('@adminjs/express'),
import('connect-pg-simple'),
import('express-session'),
import('@adminjs/sql'),
]).then(async ([
{ default: AdminJS },
{ default: AdminJSExpress },
{ default: Connect },
{ default: session },
{ default: { Adapter, Resource, Database } },
]) => {
const connectionString = `postgresql://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:${process.env.POSTGRES_PORT}/${process.env.POSTGRES_DB}`;
AdminJS.registerAdapter({ Database, Resource })
const db = await new Adapter('postgresql', {
connectionString: connectionString,
database: process.env.POSTGRES_DB,
}).init();
const DEFAULT_ADMIN = { email: '[email protected]', password: '@Admin123', name: 'FrogBase Admin' }
const authenticate = async (email, password) => {
if (email === DEFAULT_ADMIN.email && password === DEFAULT_ADMIN.password) return Promise.resolve(DEFAULT_ADMIN)
return null
}
const adminJS = new AdminJS({
version: { admin: true, app: process.env.PROJECT_VERSION },
rootPath: "/admin",
databases: [db],
});
const ConnectSession = Connect(session)
const sessionStore = new ConnectSession({
conObject: {
connectionString: connectionString,
/*
SSL is not supported in development
*/
ssl:
process.env.NODE_ENV === "production"
? { rejectUnauthorized: false }
: false,
},
tableName: 'adminjs',
createTableIfMissing: true,
});
const adminRouter = AdminJSExpress.buildAuthenticatedRouter(
adminJS,
{
authenticate,
cookieName: process.env.COOKIE_NAME,
cookiePassword: process.env.COOKIE_SECRET,
},
null,
{
store: sessionStore,
resave: true,
saveUninitialized: true,
secret: process.env.COOKIE_SECRET,
cookie: {
httpOnly: process.env.NODE_ENV === 'production',
secure: process.env.NODE_ENV === 'production',
},
name: process.env.COOKIE_NAME,
}
)
app.use(adminJS.options.rootPath, adminRouter)
app.use('*', unknownEndpoint);
app.use(handleError);
});
module.exports = app;
我面临的错误是
file:///Users/sabikrahat/Files/Algoramming/frogbase/frogbase/node_modules/adminjs/lib/adminjs.js:101
throw new Error('Adapter has to have both Database and Resource');
^
Error: Adapter has to have both Database and Resource
at AdminJS.registerAdapter (file:///Users/sabikrahat/Files/Algoramming/frogbase/frogbase/node_modules/adminjs/lib/adminjs.js:101:13)
at /Users/sabikrahat/Files/Algoramming/frogbase/frogbase/app.js:54:13
尝试改变这个:
{ default: { Adapter, Resource, Database } }
对此:
{ default: Adapter, Resource, Database }
此时,当 adminjs/sql 文档看起来正在将
default
导出为 Adapter
时,您将包含 3 个模块的对象分配给 default
。