如何在node.js外部初始化@replit/database

gijlo24d  于 2023-06-29  发布在  Node.js
关注(0)|答案(1)|浏览(124)

这是我档案:

const express = require('express');
const path = require('path');
const app = express();

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'Basic-signupsignin-setup', '../pages', 'index.html'));
});
app.get('/database', (req, res) => {
res.sendFile(path.join(__dirname, 'Basic-signupsignin-setup', '../pages', 'db.js'));
});
app.listen(3000, () => {
console.log('server started');
});

目录中的index.html只链接到发送db.js的'/database'

const users = new database();

function signUp(username, password, email, data) {
  users.set(username, `${username},${password},${email},${data}`).then(() => {})
}
signUp("username", "password", "email", "data")

function signin(username, password) {
  users.get(username).then(value => {
    const data1 = value.split(",");
    var username1 = data1[1];
    var password1 = data1[2];
    var email1 = data1[3]
    var data2 = data1[4]
    if (password1 == password) {
      console.log(`JSON OG: ${data}`, `:JSON NOW: ${username1}, ${password1}, ${email1}, ${data2}`);
    } else {
      console.error("wrong password");
    }
  })
}
signin("username", "password");

如果出现错误database:1 Uncaught ReferenceError: database is not defined at database:1:15 (anonymous) @ database:1,我该如何定义数据库?

jhkqcmku

jhkqcmku1#

您需要安装并导入:
在yout终端:

npm install @replit/database

在代码中:

const database = require("@replit/database")
const users = new database()

相关问题