Я новичок в Node.js, и впервые использую MongoDB.
Соединение работает, но база данных никогда не создается.
Я создал модель пользователя, потому что думал, что запись должна быть создана для создания базы данных
Я не уверен, что мне здесь не хватает.
index.js
import mongoose from 'mongoose';
//DB setup
mongoose.connect("mongodb://localhost:27017/hotel", {
useNewUrlParser: true
});
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function() {
console.log('Mongoose default connection open to ' + 'mongodb://localhost:27017/hotel');
});
// If the connection throws an error
mongoose.connection.on('error', function(err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function() {
console.log('Mongoose default connection disconnected');
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', function() {
mongoose.connection.close(function() {
console.log('Mongoose default connection disconnected through app termination');
process.exit(0);
});
});
user.js
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
//Defined models
const userSchema = new Schema({
email: { type: String, unique: true, lowercase: true },
password: String
});
//Create model class
const ModelClass = mongoose.model('user', userSchema);
//Export the model
module.exports = ModelClass;
Я также пытался добавить функцию .save (), но, похоже, не работал
ModelClass.save(function (err) {
if (err) return handleError(err);
// saved!
});