Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未使用Mongoose和Node.js创建数据库_Node.js_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

未使用Mongoose和Node.js创建数据库

未使用Mongoose和Node.js创建数据库,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,我是Node.js新手,这是我第一次使用MongoDB 连接似乎正常,但从未创建数据库 我创建了用户模型,因为我认为需要创建一个记录,以便创建数据库 我不确定我错过了什么 index.js import mongoose from 'mongoose'; //DB setup mongoose.connect("mongodb://localhost:27017/hotel", { useNewUrlParser: true }); // CONNECTION EVENTS // Whe

我是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);
  });
});
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;
user.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);
  });
});
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!
});

尝试用原始值初始化monogoose模型,然后保存它。i、 e

const ModelClass = mongoose.model('user', userSchema);
var user = new ModelClass({ email: 'Rover@rover.in', password: 'dog' });
// Now user.save function

尝试用原始值初始化monogoose模型,然后保存它。i、 e

const ModelClass = mongoose.model('user', userSchema);
var user = new ModelClass({ email: 'Rover@rover.in', password: 'dog' });
// Now user.save function

您的意思是创建了
用户模型,并将其添加到数据库中。但是,如果您的数据库
hotel
未创建,那么您的用户模型保存在哪个数据库中?您是否得到任何信息error@AfridaAnzumAesha对不起,也许我解释得不对,但我不是这么想的。请检查我的问题更新。@mehta rohan nope…:/@mehta rohan ahaaa你的意思是这样的好吧,让我试试看,会让你知道是否有效..你是说
用户模型被创建并添加到数据库中。但是,如果您的数据库
hotel
未创建,那么您的用户模型保存在哪个数据库中?您是否得到任何信息error@AfridaAnzumAesha对不起,也许我解释得不对,但我不是这么想的。请检查我的问题更新。@mehta rohan nope…:/@mehta rohan ahaaa你的意思是这样的好吧,让我试试,如果有效,我会告诉你的……谢谢!这就是解决办法,谢谢!这就是解决办法。