2

我正在使用 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"
  ]
}
4

1 回答 1

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)
于 2018-08-23T05:35:06.777 回答