猫鼬模式的类型有误

时间:2018-10-12 00:37:56

标签: javascript mongodb mongoose

我正在使用mongoose ODM使用mongodb开发节点应用程序。类型引用驻留在不同文件中的架构时出现错误。

在companyQuery.js文件中有以下代码:

const mongoose = require('mongoose');
const Company = require('./company');
const connectionString = 'mongodb://localhost/company'

mongoose.connect(connectionString);

mongoose.connection.on('connected', () => {
    console.log('Mongoose connected');
})
mongoose.connection.on('disconnected', () => {
    console.log('Mongoose is disconnected');
})
mongoose.connection.on('error', (err) => {
    console.log(err, 'mongoose error');
})

引用文件company.js:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const companySchema = new Schema({
        name: {
    type: String, 
    required: true
},
founded: Date,
employees: Number,
active: true;
products: [String],
ceo: {
    name: String,
    age: Number
}
});
const Company = mongoose.model('Company', companySchema);
module.exports = Company;

这是我得到的错误:

/Users/ashley/salty-sardines/mongoose-3/node_modules/mongoose/lib/schema.js:696
    throw new TypeError('Undefined type ' + name + ' at ' + path +
    ^

TypeError: Undefined type 'undefined' at 'active'
  Did you try nesting Schemas? You can only nest using refs or arrays.
    at Function.Schema.interpretAsType (/Users/ashley/salty-sardines/mongoose-3/node_modules/mongoose/lib/schema.js:696:11)
    at Schema.path (/Users/ashley/salty-sardines/mongoose-3/node_modules/mongoose/lib/schema.js:545:29)
    at Schema.add (/Users/ashley/salty-sardines/mongoose-3/node_modules/mongoose/lib/schema.js:407:12)
    at new Schema (/Users/ashley/salty-sardines/mongoose-3/node_modules/mongoose/lib/schema.js:110:10)
    at Object.<anonymous> (/Users/ashley/salty-sardines/mongoose-3/company.js:3:44)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/ashley/salty-sardines/mongoose-3/companyQuery.js:2:17)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

该问题已从我的存储库中的三台不同的计算机上复制,并且我已经用相同的结果重写了四次代码。如果删除const Company = require('./company');并将信息合并到一张纸上,则可以正常使用。任何帮助将不胜感激,因为我已经阅读了所有其他有关此类错误的文章,但都没有为我提供解决方案。

1 个答案:

答案 0 :(得分:1)

可能您只需替换一下:

active: {
  type: Boolean,
  default: true,
}

与此:

mc.thePlayer.swingItem();