草庐IT

node.js - 嵌入模式给出错误

coder 2023-10-27 原文

我试图将模式嵌入到我创建的其他模式中,但我不断收到此错误:

我不完全确定这里出了什么问题,但我想做的是在用户模式中存储对我的事件模式和兴趣模式的引用。如果有人能告诉我我做错了什么,那将非常感谢!

编辑:我现在收到一个新错误:

   /Users/Dynee/node_modules/mongoose/lib/schema.js:421
  throw new TypeError('Invalid value for schema Array path `' + prefix + key + '`');
  ^

TypeError: Invalid value for schema Array path `eventsHosted`
at Schema.add (/Users/Dynee/node_modules/mongoose/lib/schema.js:421:13)
at new Schema (/Users/Dynee/node_modules/mongoose/lib/schema.js:99:10)
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/User.js:5:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/Dynee/Documents/eventure-rest-backend/Models/Event.js:2:43)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)

我的用户架构

var mongoose = require('mongoose');
var EventSchema = require('../Models/Event').schema;
var InterestSchema = require('../Models/Interest').schema;

var UserSchema = new mongoose.Schema({
    email: String,
    password: String,
    eventsHosted: [EventSchema],
    eventsAttended: [EventSchema],
    currentlyAttending: [EventSchema],
    currentlyHosting: [EventSchema],
    profileImage: String,
    interests: [InterestSchema],
    followers: [UserSchema],
    following: [UserSchema]

});

module.exports = mongoose.model('User', UserSchema);

我的事件架构

var mongoose = require('mongoose');
var UserSchema = require('../Models/User').schema;

var EventSchema = new mongoose.Schema({
    title: String,
    description: String,
    location: String,
    attendees: [UserSchema],
    date: String,
});

module.exports = mongoose.model('Event', EventSchema);

我的兴趣架构

var mongoose = require('mongoose');

var InterestSchema = new mongoose.Schema({
    name: String
});

module.exports = mongoose.model('Interest', InterestSchema);

最佳答案

这是因为您在模块中导出的不是Schemas,它们是Models。当您执行 var EventSchema = require('../Models/Event'); 时,您需要的是 Event Model,而不是 Event Schema .要从您的模型访问底层架构,您可以执行以下操作:

var EventSchema = require('../Models/Event').schema;
var InterestSchema = require('../Models/Interest').schema;
var UserSchema = require('../Models/User').schema;

当您引用属于另一个集合的文档时,您也会遇到问题,这应该可行:

事件模型:

var mongoose = require('mongoose');
var UserSchema = require('./User').schema;

var EventSchema = new mongoose.Schema({
    title: String,
    description: String,
    location: String,
    attendees: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    date: String
});

module.exports = mongoose.model('Event', EventSchema);

兴趣模型:

var mongoose = require('mongoose');

var InterestSchema = new mongoose.Schema({
    name: String
});

module.exports = mongoose.model('Interest', InterestSchema);

用户模型:

var mongoose = require('mongoose');
var EventSchema = require('./Event').schema;
var InterestSchema = require('./Interest').schema;

var UserSchema = new mongoose.Schema({
    email: String,
    password: String,
    eventsHosted: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    currentlyAttending: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    currentlyHosting: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
    profileImage: String,
    interests: [{ type: Schema.Types.ObjectId, ref: 'Interest' }],
    followers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    following: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});

module.exports = mongoose.model('User', UserSchema);

How to reference another schema in my Mongoose schema? http://mongoosejs.com/docs/populate.html

关于node.js - 嵌入模式给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43024285/

有关node.js - 嵌入模式给出错误的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  9. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  10. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

随机推荐