Я пытаюсь создать специальный mongoose createConnection. Node.js:
MyModel = conn.model('Profile', profileSchema),
profileSchema не определен.
Но где я ошибся?
//my db.js
const mongoose = require('mongoose');
const conn = mongoose.createConnection = ("mongodb://localhost:27017/myDatabase"),
MyModel = conn.model('Profile', profileSchema),
m = new MyModel;
m.save(); //works;
if (process.env.NODE_ENV === 'production') {
conn = process.env.MONGODB_URI;
}
require('./profiles)
Здесь остальная часть моей модели:
// my profile.js
// JavaScript source code
const mongoose = require('mongoose');
const profileSchema = new mongoose.Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
});
var MyModel = mongoose.model('Profile', profileSchema);
В коде есть много проблем. Сначала узнайте, как работает модуль в javascript. Вам нужно экспортировать profileSchema
для использования внутри app.js, это должно быть,
const mongoose = require('mongoose');
const profileSchema = new mongoose.Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
});
module.exports = mongoose.model('Profile', profileSchema);
то необходимо импортировать profileSchema, который находится внутри профиля, зависит от вашего пути к файлу.
const profileSchema = require('./model/profile.js');
Что вам нужно сделать, это
// my profile.js
// JavaScript source code
const mongoose = require('mongoose');
const profileSchema = new mongoose.Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
});
module.exports = mongoose.model('Profile', profileSchema);
и в вашем файле app.js, как это
const profileInfo= require('./../model/profile.js');