奇怪的Mongoose schema.js错误- `options`不能用作模式路径名

ttvkxqim  于 2024-01-08  发布在  Go
关注(0)|答案(2)|浏览(159)

在我的模式中,如果我在metrics : [ { options : {} } ]中有options,那么我得到:

  1. /home/one/cloudimageshare-monitoring/project/node_modules/mongoose/lib/schema.js:282
  2. throw new Error("`" + path + "` may not be used as a schema pathname");
  3. ^
  4. Error: `options` may not be used as a schema pathname

字符串
但是如果把options改成任何其他的单词.比如qoptions.那么错误就消失了。为什么会发生这种情况呢?

  1. var mongoose = require('mongoose');
  2. var Schema = mongoose.Schema;
  3. var FilesystemSchema = new mongoose.Schema({
  4. timeStamp : { type : Date, index: true },
  5. avaiable : Boolean,
  6. status : String,
  7. metrics : [
  8. { options : {
  9. data : String,
  10. type : String,
  11. unit : String
  12. }
  13. },
  14. { freeFiles : {
  15. data : Number,
  16. type : String,
  17. unit : String
  18. }
  19. },
  20. { total : {
  21. data : Number,
  22. type : String,
  23. unit : String
  24. }
  25. },
  26. { avail : {
  27. data : Number,
  28. type : String,
  29. unit : String
  30. }
  31. },
  32. { free : {
  33. data : Number,
  34. type : String,
  35. unit : String
  36. }
  37. },
  38. { files : {
  39. data : Number,
  40. type : String,
  41. unit : String
  42. }
  43. },
  44. { used : {
  45. data : Number,
  46. type : String,
  47. unit : String
  48. }
  49. }
  50. ]
  51. });
  52. module.exports = FilesystemSchema;

eyh26e7m

eyh26e7m1#

Mongoose有许多不能使用的Reserved模式名称,以避免与Mongoose的内部实现冲突。文档中的列表给出了以下保留名称:

  1. on, emit, _events, db, get, set, init, isNew, errors, schema, options, modelName, collection, _pres, _posts, toObject

字符串
这些术语应该在你的模式中避免!

bfrts1fy

bfrts1fy2#

我也遇到了同样的问题,因为我们不能使用像optionserrors..

  1. new mongoose.Schema({ firstName: String }, { supressReservedKeysWarning: true })

字符串
然后,一切都如预期的那样。
下面是mongoose repo中关于Remove all reserved keywords from schema主题的问题

相关问题