我在同一个应用程序的其他集合中使用猫鼬加密没有问题,但是现在我想在新集合中使用猫鼬加密,文档已加密保存,但是当我尝试获取它们时,出现以下错误,我无法找到问题所在。
错误消息:
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));
};
答案 0 :(得分:0)
我发现问题出在保存功能上。
我在创建新架构对象时将req.body作为参数传递给。
New Analito(req.body)
。
如果我不使用猫鼬加密来加密数据,但是在加密数据时它不起作用,这很好用。
解决方案是为Schema的每个字段传递数据。
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
})