2016-10-15 2 views
0

내 파일에 대한 스키마를 생성하고 아래와 같이 호출했지만 스키마에 주석이 등록되지 않았다는 오류가 발생했습니다 ....... 이는 경로 때문입니까?몽구스에서 스키마를 등록하고 호출하는 방법

내 스키마,

var mongoose = require('mongoose'), 
    path = require('path'), 
    config = require(path.resolve('./config/config')), 
    Schema = mongoose.Schema; 

var Commentsscheme = new Schema({ 
    articleid: { 
    type: Schema.ObjectId 
    }, 
    fromuser: { 
    type: String 
    }, 
    touser: { 
    type: String 
    }, 
    comment: { 
    type: String 
    } 
}); 

mongoose.model('comments', Commentsscheme); 

내 JS,

var path = require('path'), 
    mongoose = require('mongoose'), 
    passport = require('passport'), 
    Comments = mongoose.model('comments'); 

/* ------ Inserting a comment ------ */ 
exports.insertcomment = function (req, res) { 
    var comments = new Comments(req.body); 
    console.log(comments) 
    comments.status = 1; 
    var data = {}; 
    comments.save(function (err,resl) { 
    if (err) { 
     console.log(err); 
     return err; 
    } 
    data = { status: false, error_code: 0, message: 'Unable to insert' }; 
    if (resl) { 
     data = { status: true, error_code: 0,result: resl, message: 'Inserted successfully' }; 
    } 
     res.json(data); 
    }); 
}; 

나는 내 파일과 아래를 호출하기위한 스키마를 생성하지만 스키마가 의견 등록되지 않는다는 오류를 말한다 .... .... 어느 누구도 도움을 제안 할 수 있습니다 ........................

답변

1

다음과 같이 모델을 내 보낸 다음 경로 파일 또는 호출 파일

module.exports = mongoose.model ('comments', Commentsscheme);

이제 의견이 필요하며 의견을 저장하십시오.

+0

다음과 같이이 스키마를 사용하는 파일 comment.js입니다 ............ – MMR

+0

업데이트 된 코드를 게시하십시오. – Sachin

+0

Sachin, – MMR

1

위의 두 코드가 두 개의 다른 파일과 같은 폴더에 있다고 가정합니다. 및 스키마 파일 이름은

var mongoose = require('mongoose'), 
    path = require('path'), 
    config = require(path.resolve('./config/config')), 
    Schema = mongoose.Schema; 

var Commentsscheme = new Schema({ 
    articleid: { 
    type: Schema.ObjectId 
    }, 
    fromuser: { 
    type: String 
    }, 
    touser: { 
    type: String 
    }, 
    comment: { 
    type: String 
    } 
}); 

module.exports = mongoose.model('Comment', Commentsscheme); 

및 기타 JS에서

이 오류가 계속 여전히

var path = require('path'), 
    mongoose = require('mongoose'), 
    passport = require('passport'), 
    // here you need to put the path/name of the file so that module will load. 
    Comments = require('comment.js'); 


/* ------ Inserting a comment ------ */ 
exports.insertcomment = function (req, res) { 
    var comments = new Comments(req.body); 
    console.log(comments) 
    comments.status = 1; 
    var data = {}; 
    comments.save(function (err,resl) { 
    if (err) { 
     console.log(err); 
     return err; 
    } 
    data = { status: false, error_code: 0, message: 'Unable to insert' }; 
    if (resl) { 
     data = { status: true, error_code: 0,result: resl, message: 'Inserted successfully' }; 
    } 
     res.json(data); 
    }); 
}; 
관련 문제