Tuesday 23 May 2017

Error Is not a Function

I have a file to search some sequences but when I call the function I get the error:

TypeError: utilitiesFunctions.secuencia is not a function

My function:

const utilitiesFunctions = {};
const counterModel = require('./database/models/parameters/counter');

utilitiesFunctions.sequencia = (modelo, id, callback) => {
  console.log('Entra Funcion Sequencia');
  var codigo;
  counterModel.findByIdAndUpdate({
    _id: id
  }, {
    $inc: {
      seq: 1
    }
  }, function(error, sequencia) {
    if (error) {
      callback(error, sequencia);
    } else {
      var codigo = sequencia.seq;
      if (codigo < 10) {
        var codigo = sequencia.suffix + '0' + sequencia.seq;
      } else {
        var codigo = sequencia.suffix + sequencia.seq;
      }
      callback(error, codigo);
    }
  });

}

module.exports = utilitiesFunctions;

I call the function in the Pre save hook in some models:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const utilitiesFunctions = require('./../../../utilities.js');

const wareHouseSchema = Schema({
  code: {
    type: String,
    //required: [true,'Codigo no puede estar vacio']
  },
  name: {
    type: String,
    required: [true, 'Nombre no puede estar vacio']
  },
  description: {
    type: String
  },
  isVirtual: {
    type: Boolean,
    default: false
  }
}, {timestamps: true});

wareHouseSchema.pre('save', function(next) {
  var doc = this;
  utilitiesFunctions.secuencia('warehouses', 'warehouseCode', (err, sequencia) => {
    if (err) {
      doc.code = 'D0';
    } else {
      var seq = sequencia.codigo;
      console.log('Secuencia: ' + seq);
      doc.code = seq;
    }
  });
  next();
});

const wareHouse = module.exports = mongoose.model('warehouses', wareHouseSchema);

Why I get this error? Or how is the right way to call this type of functions?

Thanks in Advance



via joselegit

No comments:

Post a Comment