2016-10-15 5 views
0

Ich habe ein Schema für meine Datei erstellt und rufe es wie folgt auf, aber es heißt Fehler, dass Schema nicht für Kommentare registriert ist ........ Liegt das an Pfad?Wie man ein Schema in mongoose registriert und anruft

Mein Schema,

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); 

Meine 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); 
    }); 
}; 

Ich habe Schema für meine Datei und es wie unten aufrufen, aber es sagt, dass Fehler-Schema nicht für Kommentare registriert ist .... .... kann jemand bitte vorschlagen Hilfe, ........................

Antwort

1

Exportieren Sie Ihr Modell wie unten und dann in der Route-Datei erfordern oder Aufruf der Datei

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

Jetzt benötigen Sie es und verwenden Sie, um Kommentare zu speichern.

+0

noch Fehler verwenden ............ – MMR

+0

Geben Sie den aktualisierten Code ein. – Sachin

+0

Sachin, wo wurde aktualisiert – MMR

1

Angenommen, dass oben zwei Codes in zwei verschiedenen Dateien und in demselben Ordner sind. und Schema Dateiname ist comment.js

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); 

und in anderen js Datei, die Sie dieses Schema

wie folgt weiter
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); 
    }); 
};