node.js - 创建使用 Number 的模式时出现 Mongoose TypeError

标签 node.js mongodb mongoose

我有一个使用数字的模式,如下所示。

var mongoose = require('mongoose')
    , Schema = mongoose.Schema
    , ObjectId = Schema.Types.ObjectId
    , Mixed = Schema.Types.Mixed
    , Number = Schema.Types.Number
    , Date = Schema.Types.Date

var ItemSchema = new Schema({
    quantity:{type:Number, required:true, min:1},
    category:{type:String, required:true, enum:categoryList},
    description:{type:String, required:true},
    cost:{type:Number, required:true, min:0, default:0},
    appraisedValue:{type:Number, min:0, default:0},
    appraisedOn:Date,
    purchasedOn:Date,
    purchasedFrom:String,
    brand:String,
    dynamicFieldList:[DynamicFieldSchema]
})
exports.ItemSchema = ItemSchema
exports.Item = mongoose.model('Item', ItemSchema)

但是当我尝试运行我的应用程序时,我收到类型错误:“数量”处未定义的类型。如果数量值被注释掉,这也会以“成本”发生。

堆栈跟踪是:

C:\Users\a\Documents\Git\has-inventory.js\node_modules\mongoose\lib\schema.js:325
throw new TypeError('Undefined type at `' + path +

TypeError: Undefined type at `quantity`
  Did you try nesting Schemas? You can only nest using refs or arrays.
at Function.Schema.interpretAsType (C:\Users\a\Documents\Git\has-inventory.js\node_modules\mongoose\lib\schema.js:325:11)
at Schema.path (C:\Users\a\Documents\Git\has-inventory.js\node_modules\mongoose\lib\schema.js:268:29)
at Schema.add (C:\Users\a\Documents\Git\has-inventory.js\node_modules\mongoose\lib\schema.js:200:12)
at new Schema (C:\Users\a\Documents\Git\has-inventory.js\node_modules\mongoose\lib\schema.js:69:10)
at Object.<anonymous> (C:\Users\a\Documents\Git\has-inventory.js\domain\inventory.js:34:18)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)

最佳答案

删除这两行:

, Number = Schema.Types.Number
, Date = Schema.Types.Date

应该在模式定义中使用内置的 JavaScript NumberDate 类型。

关于node.js - 创建使用 Number 的模式时出现 Mongoose TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12522223/

相关文章:

node.js - Express错误: EMFILE,打开的文件太多

java - Mongo 将 Document 转换为 DBObject

node.js - node-mongo-native 和 mongo-shell 在给定相同查询的情况下返回不同的结果

node.js - NodeJs 与 Mongoose - 嵌套查询异步问题

javascript - 你能有多种方法来运行 Javascript 中的函数吗?

node.js - Expressjs : Processing errors from DB operations (next is undefined)

javascript - AngularJS 模块加载栏不起作用

php - 正则表达式 MongoDB PHP 查询

linux - 写入许多文件的替代方案。 MongoDB?

node.js - 如何只更新一次 MongoDB 文档字段?