簡體   English   中英

貓鼬-預保存增量功能不觸發

[英]Mongoose - Pre Save Increment Function not triggering

我正在數據庫中創建一個新文檔,在保存之前,我想增加一個變量,但是pre save方法無法執行。

提案方案:

const Counter = require('./counter');

proposalSchema.pre('save', function(next) {

  let doc = this;

  Counter.findByIdAndUpdate({_id: 'entityId'},{$inc: { seq: 1}},{"upsert": true,"new": true  }, function(error, counter){

    if(error){
        return next(error);
    }

        doc.proposalNo = counter.seq;
        next();
  });
});

計數器架構:

const mongoose = require('mongoose');

const counterSchema = mongoose.Schema({

    _id: {type: String, required: true},
    seq: {type: Number, default: 5000}

});

mongoose.model('Counter', counterSchema);

我收到Internal Server Error 500

不管我做什么,增量都不會起作用。 我究竟做錯了什么?

您正在將proposalSchema與counterSchema混合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM