2017-09-16 2 views
1

전용 몽구스 createConnection을 만들려고합니다. Node.js rapports :몽구스 스키마를 찾을 수 없습니다

 MyModel = conn.model('Profile', profileSchema),

profileSchema is not defined.

하지만 어디서 잘못 되었나요?

다음

//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);
이 코드에 많은 문제가 있습니다

답변

0

, 먼저 배운다. 당신은 app.js 내에서 사용할 수있는 profileSchema 중위를 내보낼 필요가

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'); 
+0

같이 넣어 파일입니다. 나는 스스로를 배우고있다. – Tichel

0

당신의 대답을 주셔서 감사

// 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'); 
관련 문제