Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 类型引用mongoose架构时出错_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 类型引用mongoose架构时出错

Node.js 类型引用mongoose架构时出错,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在使用MongooseODM使用mongodb开发一个节点应用程序。键入引用驻留在不同文件中的架构时出错 我在user.js文件中有以下代码: var mongoose = require('mongoose'); var Trip = require('./trip'); var userSchema = mongoose.Schema({ firstName: String, lastName: String, salt: String, hash: S

我正在使用MongooseODM使用mongodb开发一个节点应用程序。键入引用驻留在不同文件中的架构时出错

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

var mongoose = require('mongoose');
var Trip = require('./trip');

var userSchema = mongoose.Schema({
    firstName: String,
    lastName: String,
    salt: String,
    hash: String,
    emailAddress: {
        type: String,
        unique: true
    },
    trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}]
});

module.exports = mongoose.model('User', userSchema);
userSchema具有对tripSchema的类型引用

我的trip.js文件中的代码是:

    var tripSchema = mongoose.Schema({
      name: String,
      location: String,
      arrivalDate: Date,
      departureDate: Date,
      type: String});

   module.exports = mongoose.model('Trip', tripSchema);
运行应用程序时,我遇到以下错误:

    /usr/lib/node_modules/mongoose/lib/schema.js:360
    throw new TypeError('Undefined type at `' + path +
          ^
TypeError: Undefined type at `trip.ref`
  Did you try nesting Schemas? You can only nest using refs or arrays.
    at Function.Schema.interpretAsType       (/usr/lib/node_modules/mongoose/lib/schema.js:360:11)
    at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29)
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12)
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:212:14)
    at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10)
    at Function.Schema.interpretAsType (/usr/lib/node_modules/mongoose/lib/schema.js:345:44)
    at Schema.path (/usr/lib/node_modules/mongoose/lib/schema.js:303:29)
    at Schema.add (/usr/lib/node_modules/mongoose/lib/schema.js:217:12)
    at new Schema (/usr/lib/node_modules/mongoose/lib/schema.js:73:10)
    at Mongoose.Schema (/usr/lib/node_modules/mongoose/lib/schema.js:53:12)

我无法找出错误的原因。如果两个模式在同一个文件中,则代码运行正常。但当我在两个不同的文件中分离出模式时,我得到了上面的错误。如何解决此错误?任何帮助都将不胜感激。

您的用户模式中有一个输入错误。你把

trips: [{Type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}]
但应该是这样

trips: [{type: mongoose.Schema.Types.ObjectId, ref: 'Trip'}]

类型
应该是小写的
类型

非常感谢您指出它!:)我浪费了一整天的时间试图找出错误的原因。我也犯了同样的错误,而且我已经检查过了,你的问题的答案不是我的问题。一旦我找到了一个解决方案,我会把它贴在这里,让其他人在看到这个页面时找到解决方案。