S
S
Sakolik2016-03-23 17:38:01
MongoDB
Sakolik, 2016-03-23 17:38:01

How to enable mongoose-auto-increment?

Is:
app.js

...
 mongoose =     require('mongoose');
 mongoose.connect(config.url);
 ...

config.js
module.exports = {
    'secret': 'ilovescotchyscotch',
    'url': 'mongodb://localhost/bd'
 };

user.js model
var mongoose = require('mongoose');

 module.exports = mongoose.model('Admin',{
    username: String,
    password: String,
    email: String,
    role: { type: String, default: "user" },
    soft_token: { type: String, default: 0 }
 });

I connect the model to index.js router
var User = require('../models/user');

 router.get('/test', function (req, res, next) {
     new User({
        //..//..//
     });
 });

How to attach the mongoose-auto-increment module here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitrii Solovev, 2016-03-24
@dimonnwc3

do not export the Admin model immediately, but save it to a variable, for example, AdminSchema:

let AdminSchema  = new mongoose.Schema({
  username…
  …..
});

and connect as a regular plugin:
and after that already export Admin
let Admin = mongoose.model('Admin', accountSchema);
modules.exports = Admin;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question