【问题标题】:expressjs, mongoose, Schema errorexpressjs,猫鼬,架构错误
【发布时间】:2015-05-03 07:18:33
【问题描述】:

所以。我正在尝试将 mongoDB 与使用 mongoose 的 expressjs 项目一起使用,但出现此错误。

throw new mongoose.Error.MissingSchemaError(name);
        ^
MissingSchemaError: Schema hasn't been registered for model "device".

我也是新来的,但这是我的 index.js 的源代码,其中我收到了 var device 的错误

var mongoose = require('mongoose');
var device = mongoose.model('device', device);

/* GET */
router.get('/', function(req, res) {
    new device({
        device_id    : "Something happened"
      }).save( function( err, device, count ){
        res.redirect( '/' );
    });     
});

这是我在 app.js 中的代码:

//mongo start
var mongo = require('mongodb');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var device = new Schema({
    device_id : String
});

var device = mongoose.model('device', device);

var db = mongoose.connect('mongodb://127.0.0.1:27017/database');

mongoose.connection.once('connected', function() {
    console.log("Connected to database")
});
//mongo end

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    您需要将架构传递给 .model-call,而不是模型。在您的情况下,您似乎正在重新声明“设备”以包含模型。

    我还(作为旁注)鼓励您将此架构放在单独的文件中并导出该架构。

    请参阅猫鼬教程以获取示例:mongoose schemas

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2013-06-21
      • 1970-01-01
      • 2020-09-04
      • 2021-08-19
      • 2012-12-03
      • 2017-06-15
      • 2012-07-30
      • 1970-01-01
      相关资源
      最近更新 更多