【问题标题】:How to create database list for every user with a different username and being capable of multiple people on at the same time?如何为每个具有不同用户名并能够同时有多个人的用户创建数据库列表?
【发布时间】:2021-03-02 18:50:14
【问题描述】:

我正在尝试制作一个应用程序并且它使用数据库,我遇到的问题或墙壁是我一次只能有一个连接,而不是多个连接,否则它会给出错误并且不起作用。我将用户名存储在一个变量中,即var user = "";,一次只能有一个连接。

var user = "";

const userSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    password: String,
});

const ItemSchema = new mongoose.Schema({
    person: String,
    date: String,
    loc: String,
    title: String,
    passage: String,
    file: String
});

const User = mongoose.model("User", userSchema);
const Item = mongoose.model("Item", ItemSchema);

const item1 = new Item({
    person: user,
    date: "String",
    loc: "String",
    title: "String",
    passage: "String",
    file: "String"
});

const defaultItems = [item1];

//item1.save();
// user1.save();

app.get("/", function (req, res) {

    res.render("home");

});

app.get("/loading", function (req, res) {

    res.render("loading");

});

app.get("/signup", function (req, res) {

    res.render("signup");

});

app.get("/login", function (req, res) {

    res.render("login");

});

app.get("/workspace", function (req, res) {

    Item.find({ person: user }, function (err, foundItems) {

        if (foundItems.length == 0) {
            Item.insertMany(defaultItems, function (err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log("Added items");
                }
            });
            res.redirect("/workspace");

        } else {
            res.render("workspace", { itemList: foundItems });
        }
    });


});

app.post("/login", function (req, res) {

    const email = req.body.email;
    const password = req.body.password;

    User.findOne({ email: email }, function (err, foundUser) {
        if (err) {
            console.log(err);
        } else {
            if (foundUser) {
                if (foundUser.password == password) {
                    console.log("Loged In");
                    res.redirect("/workspace");
                    user = email;
                }
            }
        }
    });
});

app.post("/signup", function (req, res) {

    const firstName = req.body.firstName;
    const lastName = req.body.lastName;
    const email = req.body.email;
    const password = req.body.password;

    const User1 = mongoose.model("User", userSchema);

    const user2 = new User1({
        first_name: firstName,
        last_name: lastName,
        email: email,
        password: password,
    });

    user2.save();

    user = email;

    console.log("Done");

    res.redirect("/workspace");
});

app.post("/workspace", function (req, res) {

    const date = req.body.input1;
    const location = req.body.input2;
    const title = req.body.input3;
    const passage = req.body.input4;
    const file = req.body.input5;

    const item = new Item({
        person: user,
        date: date,
        loc: location,
        title: title,
        passage: passage,
        file: file
    });

    item.save();

    res.redirect("/workspace");
});

有没有一种方法可以让多人同时连接并使用该应用,并为每个用户使用不同的数据库?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    一个粗略的想法。

    在建立 MongoDB 连接时,为每个访问者生成一个唯一的 ID,并根据 ID 更改 DB 的名称。请参考以下代码:

    const mongoose = require('mongoose');
    const uuid     = require('uuid');
    const dbName   = uuid();
    // connecting to mongo database
    mongoose.connect('mongodb://127.0.0.1/' + dbName, { useNewUrlParser: true, useFindAndModify: false ,useUnifiedTopology: true }, function (err) {
        if(err) {
            console.log(err);
        } else {
            console.log('Successfully connected to database.');
        }
    });
    

    它将为每个新访问者创建一个新的数据库名称。这可能不是最好的解决方案。

    【讨论】:

    • 嗨,我的意思是,我希望用户一旦登录,它就会在数据库中打开他的收藏。我已经解决了,但由于某种原因,我一次只能连接一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多