我有一个 MongoDB 数据库,我正在使用 Mongoose 进行查询。
我正在使用一个集合audits来存储有关更新更改、登录失败和错误的信息。
审计模式
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;
const auditSchema = new Schema({
ref: {
type: String,
required: true
},
linkedId: {
type: Schema.ObjectId,
refPath: 'ref'
},
action: {
type: String,
enum: ['insert', 'update', 'delete', 'special']
},
/* ... lots more fields */
});
module.exports = mongoose.model('audit', auditSchema);
Run Code Online (Sandbox Code Playgroud)
使用以下查询在我的 docker 容器中查询此集合:
return Audit.find().sort({date: -1})
.skip(skipInt)
.limit(limitInt)
.populate('user')
.populate('linkedId')
.exec();
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Schema hasn't been registered for model "error". Use mongoose.model(name, schema)
Run Code Online (Sandbox Code Playgroud)
没有“错误”模式,但是我检查了集合中所有带有ref错误的条目都没有linkedId填充:
{ref: 'error', linkedId: {$exists: true}} // 0 documents
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这在我的开发系统(win 10,vscode 调试)上运行良好,但在我的本地 docker 和 aws lamda 中都出现错误。
我已经对 docker 镜像进行了完整的重新构建,以尝试确保它不是缓存问题,但错误仍然存在。
编辑
尝试了两个 docker 文件,它们都表现出相同的行为,构建在 Windows 10 机器上
FROM node:8-alpine
WORKDIR /usr/app
COPY package.json .
RUN npm i --quiet
COPY . .
RUN npm install pm2 -g
CMD ["pm2-runtime", "./bin/www"]
FROM node:12
WORKDIR /tmp
RUN wget https://downloadarchive.documentfoundation.org/libreoffice/old/5.4.7.2/deb/x86_64/LibreOffice_5.4.7.2_Linux_x86-64_deb.tar.gz -O libo.tar.gz
RUN apt update \
&& apt install -y libxinerama1 libfontconfig1 libdbus-glib-1-2 libcairo2 libcups2 libglu1-mesa libsm6 unzip \
&& tar -zxvf libo.tar.gz
WORKDIR /tmp/LibreOffice_5.4.7.2_Linux_x86-64_deb/DEBS
RUN dpkg -i *.deb
WORKDIR /usr/app
COPY package.json .
RUN rm -rf node_modules
RUN npm install --arch=x64 --platform=linux sharp
RUN npm i --quiet
COPY . .
RUN npm install pm2 -g
CMD ["pm2-runtime", "./bin/www"]
Run Code Online (Sandbox Code Playgroud)
编辑 2
我想我已经找到了差异但不是解决方案,容器显示 mongoose 版本5.9.28但是 package.json 列出^5.8.3,更新mongoose到版本5.9.28在我的本地安装上给出相同的错误,但是现在我遇到了同样的错误并且不知道如何解决这个问题。
小智 1
我不知道Docker或AWS Lambda ,但我已经在简单的Node.js Express应用程序中检查了您的auditSchema查询,并且在我的末端也重现了相同的错误。
MissingSchemaError:Schema hasn't been registered for model "error". Use mongoose.model(name, schema)
Run Code Online (Sandbox Code Playgroud)
所以问题是:aslinkedId中声明的属性意味着它将根据文档中属性的值从多个集合中填充。因此,当我们查询时,它会从集合中获取所有记录并填充每个文档。如果文档包含值,那么猫鼬会尝试寻找人口集合,而我们没有任何错误模式或错误集合,因此它将导致上述错误。auditSchemarefpath:refrefAudit.find().populate('linkedId')AuditlinkedIdref:errorerror
Audit对于集合中ref字段包含除此之外的值的每个文档,都会生成此错误error。Mongoose需要您指定为 的值的所有架构/集合都存在ref。
要解决此问题,您需要为文档ref属性中提供的每个值定义集合Audit
请参阅此以了解更多信息refpath。
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |