使用 mongoose-observer 库时出现 MissingSchemaError

MissingSchemaError while using mongoose-observer library

我有一个典型的 Node.js、express、mongoDB 和 mongoose 应用程序。

我正在尝试实现 mongoose-observer 库来监听我的 mongodb 上的变化。 查看站点:https://www.npmjs.com/package/mongoose-observer

添加此代码时(几乎与提供的示例完全一样):

mongooseObserver.register('MessageModel', 'create', function(newMessage) {
    console.log("New Message!");
    console.log(newMessage);
});

我收到以下错误:

    node_1     | /home/app/chat/node_modules/mongoose/lib/index.js:362
    node_1     |       throw new mongoose.Error.MissingSchemaError(name);
    node_1     |       ^
    nodered_1  | 28 Mar 07:32:11 - [info] Starting flows
    mongo_1    | 2017-03-28T07:32:04.562+0000 I NETWORK  [thread1] waiting for connections on port 27017
    node_1     | MissingSchemaError: Schema hasn't been registered for model "MessageModel".
    node_1     | Use mongoose.model(name, schema)
    node_1     |     at Mongoose.model (/home/app/chat/node_modules/mongoose/lib/index.js:362:13)
    node_1     |     at registerPre (/home/app/chat/node_modules/mongoose-observer/index.js:9:27)
    node_1     |     at Object.register (/home/app/chat/node_modules/mongoose-observer/index.js:38:9)
    node_1     |     at module.exports (/home/app/chat/app/routes.js:35:22)
    node_1     |     at Object.<anonymous> (/home/app/chat/server.js:83:24)
    node_1     |     at Module._compile (module.js:409:26)
    node_1     |     at Object.Module._extensions..js (module.js:416:10)
    node_1     |     at Module.load (module.js:343:32)
    node_1     |     at Function.Module._load (module.js:300:12)
    node_1     |     at Function.Module.runMain (module.js:441:10)
    node_1     |     at startup (node.js:134:18)
    node_1     |     at node.js:962:3
    mongo_1    | 2017-03-28T07:32:11.604+0000 I NETWORK  [thread1] connection accepted from 172.25.0.1:51800 #1 (1 connection now open)

此错误仅在添加 mongoose-observer 代码时发生,没有它也可以正常工作。

其他可能相关的代码

var mongooseObserver = require('mongoose-observer');
var mongoose         = require('mongoose');
var bcrypt           = require('bcrypt'),
    SALT_WORK_FACTOR = 10;

mongoose.connect('mongodb://mongo:27017/LoRaMessages'); //database name

var messageSchema = mongoose.Schema({
    DevEUI: String,
    LoRaPayload: String,
    Header: String,
    Longitude: Number,
    Latitude: Number,
    PDOP: Number,
    TMG: Number,
    SOG: Number,
    Temparature: Number,
    Time: String,
    ServerTime: Number,
    type: String,
    deviceType: String,
    batteryLevel: String,
    JoinedAndBeeperStatus: String,
    EnabledFlags: Number,
    AlarmFlags: Number,
    T_Temp: Number,
    H_Humi: Number,
    One_Temp: Number, 
    Two_Temp: Number,
    _msgid: String        
});

var MessageModel = mongoose.model('loramessages', messageSchema);  //collection name

mongooseObserver.register('MessageModel', 'create', function(newMessage) { //it fails on this line
    console.log("New Message!");
    console.log(newMessage);
});

我知道这个库没有被很多人使用,但也许我遗漏了一些明显的东西。

我已经看到另一个 post 包含此错误,但似乎还有另一个问题,因为这仅在实现特定库代码时发生。

感谢任何帮助。

编辑: 虽然@Love-Kesh 的回答是正确的。我发现此库仅用于监听 Node.js 内部的更改,未检测到外部添加的数据。

在 Observer.register

中传递 'loramessages' 而不是 'MessageModel'

如果您使用保存来创建新项目,它不会触发,请使用 Model.create({}) 来创建新记录。

//尝试保存事件侦听器是否可用

Observer.register('loramessages', 'save', function(createdUser){
    // this callback will be executed when a new user is created
    // Do something here, for example, send a email to the created user
  });