You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Mongoose加密代码报ReferenceError: encrypt is not defined如何解决?

错误产生原因

你的代码中导入mongoose-encryption模块时,将模块实例赋值给了名为mongooseEncrytion的变量,但调用userSchema.plugin()方法时传入的是从未声明定义的encrypt变量,JS运行时无法找到该变量的挂载对象,因此抛出ReferenceError: encrypt is not defined错误。
额外提示:原代码中声明的模块变量名mongooseEncrytion存在拼写错误,正确拼写应为mongooseEncryption,建议同步修正避免后续调用时因手滑拼写出现二次报错。

修复方案
  • 方案一:修改插件调用时的参数,和导入时的变量名保持一致
const mongoose = require("mongoose");
// 修正拼写错误
const mongooseEncryption = require("mongoose-encryption");

const secret = "Thisisourlittlesecret.";
userSchema.plugin(mongooseEncryption, { 
  secret: secret,
  encryptedFields: ['password'] 
});
  • 方案二:导入模块时直接命名为encrypt,适配原有调用逻辑
const mongoose = require("mongoose");
// 导入时直接命名为encrypt
const encrypt = require("mongoose-encryption");

const secret = "Thisisourlittlesecret.";
userSchema.plugin(encrypt, { 
  secret: secret,
  encryptedFields: ['password'] 
});

内容的提问来源于stack exchange,提问作者Jaideep Singh Thakur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 06:54:00