I'm getting this error while running the following code in Node the first file is the dao file where it connect to a mongodb the second is the server file and the last one is index1 file
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost:27017/base', function(err) {
if (err) {
throw err;
}
var Schema = mongoose.Schema;
var IndexSchema = new Schema({
indexp1: Number,
date:Number,
heure:String
});
var Index = mongoose.model('Index', IndexSchema);
exports.Index= mongoose.model('Index', IndexSchema);
var Points = function(date_debut,date_fin){
Index.find({}, function(err, data){
if(err) throw err;
if(data.length > 1){
res.render("index1", {datas: data})
}
else{
res.render("index1", {datas: "No names added yet!"})
}
return(datas);
});
};
module.exports = Points;
var dao = require('./daotest.js');
dao.Points(15,15);
<% for (var i = 0; i < datas.length; i++) {%>
<p> <%= datas[i].indexp1 %> </p>
<% } %>
What am I doing wrong?
Change Var
to var
. Capitalization matters for keywords.
May be you problem is in this line:
exports.Index= mongoose.model('Index', IndexSchema);
Because:
In any case if trying to assign a new object to exports ensure we do it this way.
exports = module.exports = {};
If we do it following way wrong way, module.exports will still be pointing to the object created as part of module instance.
exports = {};
As as result adding anything to the above exports object will have no effect to module.exports object and nothing will be exported or returned as part of require.
So try :
module.exports.Index= mongoose.model('Index', IndexSchema);
Hope helps you!!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.