未找到自动增量插件nextCount

时间:2018-01-09 05:57:29

标签: mongodb typescript mongoose mongoose-plugins

我正在使用mongoose-auto-increment插件,我正在使用TypeScript Node.js,我已经安装了所需的类型定义但是当我尝试获取下一次自动增量计数时,我得到了这个

Property 'nextCount' does not exist on type 'Model<Document>'

我的架构是示例中的架构:

let bookSchema = new Schema({
    author: { type: Number, ref: 'Author' },
    title: String,
    genre: String,
    publishDate: Date
});

bookSchema.plugin(autoIncrement.plugin, 'Book');
var Book = connection.model('Book', bookSchema);

//// error here
Book.nextCount(function(err, count) {
});

我的tsconfig.json就是这样

{
  "compilerOptions": {
    "types" : ["node", "socket.io"],
    "module": "commonjs",
    "experimentalDecorators": true,
    "target": "es2015",
    "lib": ["es2015", "es2017", "dom"]
  },
  "include": [
    "**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

1 个答案:

答案 0 :(得分:0)

在保存批量之前,只需使用ObjectId并为每条聊天消息设置_id

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

messages.forEach(message => {
  message._id = message._id || ObjectId()
})

// call you method which save batches
saveBatch(messages)