繁体   English   中英

在 Mongoose Schema 中使用 _id 作为属性类型时出错

[英]Error when using _id as a property type in a Mongoose Schema

我目前正在学习 MongoDB 和 mongoose。 我在猫鼬中有一个存档和一个用户架构:

存档.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);

用户.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 ,但我不知道如何在两个表之间进行引用。

作为参考,这是我的完整数据库设计或多或少的样子: 在此处输入图片说明

欢迎任何有关如何解决此问题或改进整体设计的意见。 谢谢。

我认为您在谈论的是对其他集合的引用:

author: { type: Schema.Types.ObjectId, ref: 'User' }

assigned_members: [{ type: Schema.Types.ObjectId, ref: 'User' }]

应该工作正常。

来源:猫鼬种群

我遇到了同样的问题。我导入了一个模块,它只是没有从另一个模块导出。 所以我补充说:

exports.genreSchema = genreSchema;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM