错误不是函数

时间:2017-05-23 20:16:18

标签: javascript node.js mongodb express mongoose

我有一个文件来搜索某些序列,但是当我调用该函数时,我得到错误:

  

TypeError:utilitiesFunctions.secuencia不是函数

我的功能:

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;

我在某些模型的Pre保存挂钩中调用该函数:

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

为什么我收到此错误? 或者如何正确地调用这种类型的函数?

先谢谢

1 个答案:

答案 0 :(得分:0)

拼写正确,使用'q'而不是'c':sequencia是如何定义的,但您正在调用secuencia