目前我正在学习MongoDB和mongoose。我在mongoose中有一个归档和一个用户模式:
archive.js
var mongoose = require('mongoose');
var User = require('../users/user');
var notesSchema = new mongoose.Schema({
author: User.userId,
text: String,
files:[String]
});
var archiveSchema = new mongoose.Schema({
name: String,
priority: String,
deadline: Date,
status: String,
assigned_memnbers: [User.userId],
notes: [notesSchema],
});
archiveSchema.virtual('archiveId').get(function() {
return this._id;
});
module.exports = mongoose.model('Archive', archiveSchema);user.js:
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
username: String,
mail: String,
bio: String,
password: String
});
userSchema.virtual('userId').get(function() {
return this._id;
});
module.exports = mongoose.model('User', userSchema);当我运行我的服务器时,我得到这个错误
TypeError: Invalid value for schema path `author`, got value "undefined"问题来自author: User.userId,但我不知道如何在这两个表之间进行引用。
作为参考,下面是我的完整数据库设计大致是什么样子:

欢迎任何关于如何解决这个问题或改进整体设计的意见。谢谢你。
发布于 2021-07-14 22:43:34
我也遇到了同样的问题,我导入了一个模块,只是不能从另一个模块导出。所以我加了一句:
exports.genreSchema = genreSchema;https://stackoverflow.com/questions/60060929
复制相似问题