mongoose返回Schema尚未注册model

时间:2015-11-15 15:55:30

标签: node.js mongodb express mongoose

我的快递应用程序上有以下错误,它告诉我我没有注册我的架构,但实际上我已经这样做了。

 /home/work/dashboard/node_modules/mongoose/lib/index.js:333
      throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "Devices".
Use mongoose.model(name, schema)
    at Mongoose.model (/home/work/dashboard/node_modules/mongoose/lib/index.js:333:13)
    at Object.<anonymous> (/home/work/dashboard/server/services/devices.js:6:28)
    at Module._compile (module.js:435:26)
    at normalLoader (/home/work/dashboard/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/work/dashboard/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/home/work/dashboard/server/routes/index.js:2:25)
    at Module._compile (module.js:435:26)
    at normalLoader (/home/work/dashboard/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/work/dashboard/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)

这就是我的模型的样子

   import mongoose from 'mongoose';

var Schema = mongoose.Schema;

var Devices = new Schema({

    userName: {
        type: String,
        unique: true
    },
    userId: {
        type: String,
        unique: true
    },
    devices: [{
        gatwayId: String,
        type: String
    }]
}, {
    collection: 'devices'
});

Devices = mongoose.model('Devices', Devices);

export default Devices;

这是应该调用模型的服务。

 'use strict';

import mongoose from 'mongoose';
import _ from 'lodash';

var deviceModel = mongoose.model('Devices');

最后,这就是它在主要的index.js

上的样子
import express from 'express';
import http from 'http';
import cors from 'cors';
import api from './routes';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import logger from 'morgan';
import bodyParser from 'body-parser';
import fs from 'fs';
import join from 'join';

dotenv.config();

var app = express();

app.server = http.createServer(app);

app.db = mongoose.createConnection(process.env.MONGO_URL);

app.db.on('error', console.error.bind(console, 'mongoose connection error: '));

app.db.once('open', function() {
    console.log('connected to the mongoose database');
});


// Bootstrap models
fs.readdirSync(join(__dirname, 'models')).forEach(function (file) {
  if (~file.indexOf('.js')) require(join(__dirname, 'models', file));
});


// 3rd party middleware
app.use(cors({
    exposedHeaders: ['Link']
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(logger('dev'));

// api router
app.use('/api', api);

app.server.listen(process.env.PORT || 6000);

console.log(`Started on port ${app.server.address().port}`);

export default app;

1 个答案:

答案 0 :(得分:0)

您需要import您在某处定义模型的文件。所以在你的代码中的某个地方。

import Device from './path/to/your/model/file'