簡體   English   中英

為什么在Mongoose模式中存在未定義的類型?

[英]Why is there an undefined type in my Mongoose Schema?

每次我嘗試使用npm start命令從命令行運行服務器時,都會不斷收到以下錯誤消息:

 throw new TypeError('Undefined type `' + name + '` at `' + path +
    ^

TypeError: Undefined type `P` at `0`

這是我的模塊,據說導致該問題:

var mongoose = require('mongoose');
var Currency = require('mongoose-currency').loadType(mongoose);

var Schema = mongoose.Schema;

var promoSchema = Schema({ 

  name: {
    type: String,
    required: true,
    unique: true
  },

  image: {
    type: String,
    required: true,
    unique: true
  },

  label: {
    type: String,
    required: false,
    default: ""
  },

  price: {
    type: Currency,
    required: true
  },

  description: {
    type: String,
    required: true
  }
},
{
  timestamps: true
});

var Promotions = Schema('Promotion', promoSchema);

module.exports = Promotions;

為什么會引發錯誤?

您正在混淆模式和模型。 這行是不正確的:

var Promotions = Schema('Promotion', promoSchema);

Schema應該是mongoose.model ,因為您是根據promoSchema創建模型,而不是根據Schema創建模式! 貓鼬會嘗試根據您的參數創建一個模式,從而引發錯誤。 采用:

var Promotions = mongoose.model('Promotion', promoSchema)

Mongoose文檔中了解有關模型的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM