【问题标题】:Error while saving a Document in MongoDB while using plugin mongoosastic使用插件 mongoosastic 时在 MongoDB 中保存文档时出错
【发布时间】:2015-12-07 11:56:23
【问题描述】:

我的弹性搜索服务运行良好。我正在使用 mongoosastic 插入 Elastic Search

这是我使用的代码

var mongoose = require('mongoose')
, mongoosastic = require('mongoosastic')
, Schema = mongoose.Schema

var User = new Schema({
name: String
, email: String
, city: String
})

var userModel=mongoose.model("userModel",User)

User.plugin(mongoosastic)

mongoose.connect("mongodb://localhost/myapp" ,function(err) {
if (err) throw err;

var user=new userModel()
user.name="Abhay"
user.save(function(err,user){
    console.log("err is ",err)
    console.log("user is ",user)
})
})

这会产生这个错误

/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:757
catch(err) { process.nextTick(function() { throw err}); }
^
TypeError: undefined is not a function
at model.postSave (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoosastic/lib/mongoosastic.js:398:9)
at EventEmitter. (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/lib/schema.js:749:17)
at EventEmitter.emit (events.js:129:20)
at model.Document.(anonymous function) as emit
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/lib/model.js:268:13
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/lib/model.js:127:7
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/lib/collection.js:449:5
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/lib/collection.js:593:5
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:457:9
at resultHandler (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:409:5)
at /home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:756:13
at Callbacks.emit (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)
at null.messageHandler (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:243:23)
at Socket. (/home/abhay/Documents/project/webstrom/node/personal/mongo-node-elastic/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)

注意如果我评论或删除User.plugin(mongoosastic)然后文档保存到集合成功

【问题讨论】:

  • 您是否尝试将User.plugin(mongoosastic) 移到mongoose.model() 行上方?
  • 它有效,愚蠢的错误

标签: mongodb elasticsearch mongoosastic


【解决方案1】:

您只需将User.plugin(mongoosastic) 移到mongoose.model() 行上方

...
var User = new Schema({
name: String
, email: String
, city: String
})

User.plugin(mongoosastic)          <---- move this line up here

var userModel=mongoose.model("userModel",User)
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多