错误:身份验证失败(Mongoose加密)

错误:身份验证失败(Mongoose加密),mongoose,mongoose-schema,Mongoose,Mongoose Schema,我在同一应用程序的其他集合中使用mongoose加密没有问题,但现在我想在新集合中使用它,文档被加密保存,但当我尝试获取它们时,我得到以下错误,我无法找到问题所在 错误消息: Error: Authentication failed at Object.schema.methods.authenticateSync (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:436

我在同一应用程序的其他集合中使用mongoose加密没有问题,但现在我想在新集合中使用它,文档被加密保存,但当我尝试获取它们时,我得到以下错误,我无法找到问题所在

错误消息:

Error: Authentication failed
at Object.schema.methods.authenticateSync (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:436:19)
at model.<anonymous> (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:239:47)
at Kareem.execPreSync (C:\Proyectos\LIMSp\node_modules\kareem\index.js:115:16)
at model.syncWrapper [as $__init] (C:\Proyectos\LIMSp\node_modules\kareem\index.js:232:12)
at model.Document.init (C:\Proyectos\LIMSp\node_modules\mongoose\lib\document.js:486:8)
at completeMany (C:\Proyectos\LIMSp\node_modules\mongoose\lib\helpers\query\completeMany.js:41:14)
at cb (C:\Proyectos\LIMSp\node_modules\mongoose\lib\query.js:1928:9)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:75:17
at executeCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:68:9)
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:36:13
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at completeClose (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:859:16)
at Cursor.close (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:878:12)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:35:25
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\core\cursor.js:32:5)
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");

const analitoSchema = mongoose.Schema({
  
  nombre: String,
  abreviatura: String,
  CAS: String,
  sinonimos: [String],
  grupoSustancias: [String],
  estandarInterno: {value: Boolean, viewValue: String},
  sectores: [String],
  rubros: [String],
  habilitado: {value: Boolean, viewValue: String},
  excluido: {value: Boolean, viewValue: String},
  observaciones: String
});

var encKey = "J/zNLhysm3yMP8v24+gsCZe7dccgq4pX5bxrM6X0vHM=";
var sigKey ="ZR35/JqMv0J1V7JU+WMkV2U3HOjadPS6S6DExi2lv5v1N7S1T1I6Jm33ZSHMlnjz5qJeG0j3Qqm7ECvHMSr+Mg==";

analitoSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey });

module.exports = mongoose.model("Analito", analitoSchema);
exports.save= (req, res, next) => { 
  const analito = new Analito(req.body) 
  analito.save()
  .then(documents => {       
    res.status(201).json({
      message: newSavedMessage,
      data: documents,
      error: null
    });
  })
  .catch(err => {
    errorHandler.errorHandler(err, res);
  })
};
exports.getAll= (req,res, next) => {
  Analito.find()
    .then((documents) => {
      filteredDocuments = documents.filter((data) =>data.excluido.value != true);
      res.status(200).json({
        message: successfullMessage,
        data: filteredDocuments,
        error: null,
      });
    })
    .catch((err) => errorHandler.errorHandler(err, res));
  };
保存架构代码:

Error: Authentication failed
at Object.schema.methods.authenticateSync (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:436:19)
at model.<anonymous> (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:239:47)
at Kareem.execPreSync (C:\Proyectos\LIMSp\node_modules\kareem\index.js:115:16)
at model.syncWrapper [as $__init] (C:\Proyectos\LIMSp\node_modules\kareem\index.js:232:12)
at model.Document.init (C:\Proyectos\LIMSp\node_modules\mongoose\lib\document.js:486:8)
at completeMany (C:\Proyectos\LIMSp\node_modules\mongoose\lib\helpers\query\completeMany.js:41:14)
at cb (C:\Proyectos\LIMSp\node_modules\mongoose\lib\query.js:1928:9)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:75:17
at executeCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:68:9)
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:36:13
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at completeClose (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:859:16)
at Cursor.close (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:878:12)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:35:25
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\core\cursor.js:32:5)
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");

const analitoSchema = mongoose.Schema({
  
  nombre: String,
  abreviatura: String,
  CAS: String,
  sinonimos: [String],
  grupoSustancias: [String],
  estandarInterno: {value: Boolean, viewValue: String},
  sectores: [String],
  rubros: [String],
  habilitado: {value: Boolean, viewValue: String},
  excluido: {value: Boolean, viewValue: String},
  observaciones: String
});

var encKey = "J/zNLhysm3yMP8v24+gsCZe7dccgq4pX5bxrM6X0vHM=";
var sigKey ="ZR35/JqMv0J1V7JU+WMkV2U3HOjadPS6S6DExi2lv5v1N7S1T1I6Jm33ZSHMlnjz5qJeG0j3Qqm7ECvHMSr+Mg==";

analitoSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey });

module.exports = mongoose.model("Analito", analitoSchema);
exports.save= (req, res, next) => { 
  const analito = new Analito(req.body) 
  analito.save()
  .then(documents => {       
    res.status(201).json({
      message: newSavedMessage,
      data: documents,
      error: null
    });
  })
  .catch(err => {
    errorHandler.errorHandler(err, res);
  })
};
exports.getAll= (req,res, next) => {
  Analito.find()
    .then((documents) => {
      filteredDocuments = documents.filter((data) =>data.excluido.value != true);
      res.status(200).json({
        message: successfullMessage,
        data: filteredDocuments,
        error: null,
      });
    })
    .catch((err) => errorHandler.errorHandler(err, res));
  };
获取所有文档代码:

Error: Authentication failed
at Object.schema.methods.authenticateSync (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:436:19)
at model.<anonymous> (C:\Proyectos\LIMSp\node_modules\mongoose-encryption\lib\plugins\mongoose-encryption.js:239:47)
at Kareem.execPreSync (C:\Proyectos\LIMSp\node_modules\kareem\index.js:115:16)
at model.syncWrapper [as $__init] (C:\Proyectos\LIMSp\node_modules\kareem\index.js:232:12)
at model.Document.init (C:\Proyectos\LIMSp\node_modules\mongoose\lib\document.js:486:8)
at completeMany (C:\Proyectos\LIMSp\node_modules\mongoose\lib\helpers\query\completeMany.js:41:14)
at cb (C:\Proyectos\LIMSp\node_modules\mongoose\lib\query.js:1928:9)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:75:17
at executeCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\execute_operation.js:68:9)
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:36:13
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\utils.js:129:55)
at completeClose (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:859:16)
at Cursor.close (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\cursor.js:878:12)
at C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\operations\to_array.js:35:25
at handleCallback (C:\Proyectos\LIMSp\node_modules\mongoose\node_modules\mongodb\lib\core\cursor.js:32:5)
const mongoose = require("mongoose");
const encrypt = require("mongoose-encryption");

const analitoSchema = mongoose.Schema({
  
  nombre: String,
  abreviatura: String,
  CAS: String,
  sinonimos: [String],
  grupoSustancias: [String],
  estandarInterno: {value: Boolean, viewValue: String},
  sectores: [String],
  rubros: [String],
  habilitado: {value: Boolean, viewValue: String},
  excluido: {value: Boolean, viewValue: String},
  observaciones: String
});

var encKey = "J/zNLhysm3yMP8v24+gsCZe7dccgq4pX5bxrM6X0vHM=";
var sigKey ="ZR35/JqMv0J1V7JU+WMkV2U3HOjadPS6S6DExi2lv5v1N7S1T1I6Jm33ZSHMlnjz5qJeG0j3Qqm7ECvHMSr+Mg==";

analitoSchema.plugin(encrypt, { encryptionKey: encKey, signingKey: sigKey });

module.exports = mongoose.model("Analito", analitoSchema);
exports.save= (req, res, next) => { 
  const analito = new Analito(req.body) 
  analito.save()
  .then(documents => {       
    res.status(201).json({
      message: newSavedMessage,
      data: documents,
      error: null
    });
  })
  .catch(err => {
    errorHandler.errorHandler(err, res);
  })
};
exports.getAll= (req,res, next) => {
  Analito.find()
    .then((documents) => {
      filteredDocuments = documents.filter((data) =>data.excluido.value != true);
      res.status(200).json({
        message: successfullMessage,
        data: filteredDocuments,
        error: null,
      });
    })
    .catch((err) => errorHandler.errorHandler(err, res));
  };

我发现问题出在保存功能上

在创建新模式对象时,我将req.body作为参数传递给。
新的Analito(要求正文)

如果我不使用mongoose加密对数据进行加密,但在加密数据时,它不起作用,那么这种方法效果很好

解决方案是为模式的每个字段传递数据

  const analito = new Analito({
    nombre: req.body.nombre,
    abreviatura: req.body.abreviatura,
    CAS: req.body.CAS,
    sinonimos: req.body.sinonimos,
    grupoSustancias: req.body.grupoSustancias,
    estandarInterno: {
      value: req.body.estandarInterno.value,
      viewValue: req.body.estandarInterno.viewValue
    },
    sectores: req.body.sectores,
    rubros: req.body.rubros,
    habilitado: {
      value: req.body.habilitado.value,
      viewValue: req.body.habilitado.viewValue
    },
    excluido: {
      value: req.body.excluido.value,
      viewValue: req.body.excluido.viewValue
    },
    observaciones: req.body.observaciones
  })