drudge/mongoose-timestamp

TypeError: documentSchema.plugin is not a function

paul-ley opened this issue · 1 comments

did an npm install mongoose-timestamp, implemented the plugin, as follows:

var timestamps = require('mongoose-timestamp');
var mongoose = require("mongoose");

var mongoSchema = mongoose.Schema;

var documentSchema = {
    name: String,
 isPayed: { type: Boolean, default: false }
};

documentSchema.plugin(timestamps);
module.exports = mongoose.model('documents', documentSchema);

When starting node, the compiler returns folliowing error:

PS C:\Users\paul\Programming\cli\ngEstrich_Server> node server
C:\Users\paul\Programming\cli\ngEstrich_Server\models\documents.js:65
documentSchema.plugin(timestamps);
               ^

In your code, documentSchema is a plain object, not an instance of a mongoose.Schema object. Try:

var timestamps = require('mongoose-timestamp');
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var documentSchema = new Schema({
    name: String,
 isPayed: { type: Boolean, default: false }
});

documentSchema.plugin(timestamps);
module.exports = mongoose.model('documents', documentSchema);