drudge / mongoose-timestamp

Adds createdAt and updatedAt date attributes that get auto-assigned to the most recent create/update timestamp
Other
308 stars 64 forks source link

TypeError: documentSchema.plugin is not a function #40

Closed paul-ley closed 7 years ago

paul-ley commented 7 years ago

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);
               ^
drudge commented 7 years ago

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