[英]NodeJS + Express + Mongoose(MongoDB) Database Insert Error
[英]How to create seperate database for every user who register : Nodejs , express, mongodb & mongoose
這是我的用戶架構:
import mongoose from "mongoose";
const { Schema } = mongoose;
//Address Schema
const addressSchema: mongoose.Schema = new Schema({});
//Talk about token saving planning
const userSchema: mongoose.Schema = new Schema(
{
name: {
type: String,
trim: true,
required: true,
},
username: {
type: String,
trim: true,
},
email: {
type: String,
trim: true,
required: true,
unique: true,
},
mobile_number: {
type: Number,
trim: true,
required: true,
unique: true,
},
password: {
type: String,
required: true,
min: 9,
max: 30,
},
picture: {
//AWS return object as response after upload
type: {},
//we will not provide any default
//default will be set in frontend
},
},
{ timestamps: true }
);
export default mongoose.model("User", userSchema);
當 newuser 在 Nodejs & Express mongoose 中注冊時,有什么方法或任何人知道如何創建新數據庫。
我正在制作 crm ,我想為每個注冊的用戶創建單獨的數據庫。
var MongoClient = require('mongodb').MongoClient;
//Create a database named "userName":
userCollection.foreach(name => {
var url = "mongodb://localhost:27017/"+ name;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
}
是的,它可能在 mySql 中! 你可以試試 mongoose sequelize node js
const sequelizedb = new Sequelize("", MysqlInfo.username, MysqlInfo.password, {
host: MysqlInfo.dbhost,
dialect: "mysql"
});
let dbcreation = "CREATE DATABASE `" + username + "`;";
sequelizedb.query(dbcreation).then(res => {
const newDBconnection = new Sequelize(
"mysql://" +
MysqlInfo.username +
":" +
MysqlInfo.password +
"@" + MysqlInfo.dbhost + ":3306/" +
schooldbname
);
newDBconnection
.authenticate()
.then(() => {
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.