MongooseError: Model.findOne() 不再接受回调 && userSchema.plugin(autoIncrement.plugin, 'user');

问题描述 投票:0回答:1

The screenshot of error page

import mongoose from "mongoose";
import autoIncrement from "mongoose-auto-increment";

const userSchema = mongoose.Schema({
    name: String,
    username: String,
    email: String,
    phone: String
})

autoIncrement.initialize(mongoose.connection);
userSchema.plugin(autoIncrement.plugin, 'user'); //error occurs in this line, when I comment this line, rest work fine.

const user = mongoose.model('user', userSchema);

export default user;

在给定的代码中,当我注释第 12 行时,如下所示。

userSchema.plugin(autoIncrement.plugin, 'user');

然后它工作正常但是当我尝试使用 `userSchema.plugin(autoIncrement.plugin, 'user');`.\ 它给出了可以在终端部分下的屏幕截图中清楚地看到的错误。

报错信息如下

MongooseError: Model.findOne() no longer accepts a callback
    at Function.findOne (D:\personal project\rough\CRUD\server\node_modules\mongoose\lib\model.js:2129:11)
    at exports.plugin (D:\personal project\rough\CRUD\server\node_modules\mongoose-auto-increment\index.js:73:19)      
    at Schema.plugin (D:\personal project\rough\CRUD\server\node_modules\mongoose\lib\schema.js:1884:3)
    at file:///D:/personal%20project/rough/CRUD/server/schema/user-schema.js:12:12
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v19.1.0
[nodemon] app crashed - waiting for file changes before starting...
  1. 我卸载并重新安装了
    mongoose-auto-increment
    .
  2. 我已经检查过这个
    Function.findOne (node_modules\mongoose\lib\model.js:2129:11)
    但没有找到任何我需要修改的东西。
mongoose-schema mongoose-plugins mongoose-models mongoose-auto-increment
1个回答
0
投票

我在将旧项目迁移到 mongodb 6.0 时遇到同样的问题

还有更多问题

Model.prototype.save() no longer accepts a callback
.

我只是让我的项目运行起来,而不是确保功能正确。

我已经找到原始回购这里.

73号线使用了

findOne
功能。我认为这就是问题所在。但是不再维护该库。也许我们应该更改为其他库或导入代码并修复它。

我正在将它导入到本地文件并修复它。
  1. 删除
    package.json
  2. 中的依赖
"mongoose-auto-increment": "^5.0.1",

添加依赖。

"extend": "^3.0.0",
  1. 复制代码并修复它。 创建文件:
    mongoose-auto-increment.js
    .
// Module Scope
var mongoose = require('mongoose'),
    extend = require('extend'),
    counterSchema,
    IdentityCounter;

// Initialize plugin by creating counter collection in database.
exports.initialize = function (connection) {
    try {
        IdentityCounter = connection.model('IdentityCounter');
    } catch (ex) {
        if (ex.name === 'MissingSchemaError') {
            // Create new counter schema.
            counterSchema = new mongoose.Schema({
                model: { type: String, require: true },
                field: { type: String, require: true },
                count: { type: Number, default: 0 }
            });

            // Create a unique index using the "field" and "model" fields.
            counterSchema.index({ field: 1, model: 1 }, { unique: true, required: true, index: -1 });

            // Create model using new schema.
            IdentityCounter = connection.model('IdentityCounter', counterSchema);
        }
        else
            throw ex;
    }
};

// The function to use when invoking the plugin on a custom schema.
exports.plugin = function (schema, options) {

    // If we don't have reference to the counterSchema or the IdentityCounter model then the plugin was most likely not
    // initialized properly so throw an error.
    if (!counterSchema || !IdentityCounter) throw new Error("mongoose-auto-increment has not been initialized");

    // Default settings and plugin scope variables.
    var settings = {
        model: null, // The model to configure the plugin for.
        field: '_id', // The field the plugin should track.
        startAt: 0, // The number the count should start at.
        incrementBy: 1, // The number by which to increment the count each time.
        unique: true // Should we create a unique index for the field
    },
        fields = {}, // A hash of fields to add properties to in Mongoose.
        ready = false; // True if the counter collection has been updated and the document is ready to be saved.

    switch (typeof (options)) {
        // If string, the user chose to pass in just the model name.
        case 'string':
            settings.model = options;
            break;
        // If object, the user passed in a hash of options.
        case 'object':
            extend(settings, options);
            break;
    }

    if (settings.model == null)
        throw new Error("model must be set");

    // Add properties for field in schema.
    fields[settings.field] = {
        type: Number,
        require: true
    };
    if (settings.field !== '_id')
        fields[settings.field].unique = settings.unique
    schema.add(fields);

    // Find the counter for this model and the relevant field.
    IdentityCounter.findOne(
        { model: settings.model, field: settings.field })
        .then(counter => {
            if (!counter) {
                // If no counter exists then create one and save it.
                counter = new IdentityCounter({ model: settings.model, field: settings.field, count: settings.startAt - settings.incrementBy });
                counter.save()
                    .then(() => {
                        ready = true;
                    });
            }
            else {
                ready = true;
            }
        })
        .catch(err => {
            // TODO
        });

    // Declare a function to get the next counter for the model/schema.
    var nextCount = function (callback) {
        IdentityCounter.findOne({
            model: settings.model,
            field: settings.field
        })
            .then((counter) => {
                callback(null, counter === null ? settings.startAt : counter.count + settings.incrementBy);
            })
            .catch(err => {
                callback(err);
            })
    };
    // Add nextCount as both a method on documents and a static on the schema for convenience.
    schema.method('nextCount', nextCount);
    schema.static('nextCount', nextCount);

    // Declare a function to reset counter at the start value - increment value.
    var resetCount = function (callback) {
        IdentityCounter.findOneAndUpdate(
            { model: settings.model, field: settings.field },
            { count: settings.startAt - settings.incrementBy },
            { new: true }, // new: true specifies that the callback should get the updated counter.
        )
            .then(() => {
                callback(null, settings.startAt);
            })
            .catch(err => {
                callback(err);
            });
    };
    // Add resetCount as both a method on documents and a static on the schema for convenience.
    schema.method('resetCount', resetCount);
    schema.static('resetCount', resetCount);

    // Every time documents in this schema are saved, run this logic.
    schema.pre('save', function (next) {
        // Get reference to the document being saved.
        var doc = this;

        // Only do this if it is a new document (see http://mongoosejs.com/docs/api.html#document_Document-isNew)
        if (doc.isNew) {
            // Declare self-invoking save function.
            (function save() {
                // If ready, run increment logic.
                // Note: ready is true when an existing counter collection is found or after it is created for the
                // first time.
                if (ready) {
                    // check that a number has already been provided, and update the counter to that number if it is
                    // greater than the current count
                    if (typeof doc[settings.field] === 'number') {
                        IdentityCounter.findOneAndUpdate(
                            // IdentityCounter documents are identified by the model and field that the plugin was invoked for.
                            // Check also that count is less than field value.
                            { model: settings.model, field: settings.field, count: { $lt: doc[settings.field] } },
                            // Change the count of the value found to the new field value.
                            { count: doc[settings.field] },
                        )
                            .then((err) => {
                                // Continue with default document save functionality.
                                next();
                            })
                            .catch(err => {
                                return next(err);
                            });
                    } else {
                        // Find the counter collection entry for this model and field and update it.
                        IdentityCounter.findOneAndUpdate(
                            // IdentityCounter documents are identified by the model and field that the plugin was invoked for.
                            { model: settings.model, field: settings.field },
                            // Increment the count by `incrementBy`.
                            { $inc: { count: settings.incrementBy } },
                            // new:true specifies that the callback should get the counter AFTER it is updated (incremented).
                            { new: true },
                            // Receive the updated counter.
                        )
                        .then((err, updatedIdentityCounter) => {
                            // If there are no errors then go ahead and set the document's field to the current count.
                            doc[settings.field] = updatedIdentityCounter.count;
                            // Continue with default document save functionality.
                            next();
                        })
                        .catch(err => {
                            return next(err);
                        })
                    }
                }
                // If not ready then set a 5 millisecond timer and try to save again. It will keep doing this until
                // the counter collection is ready.
                else
                    setTimeout(save, 5);
            })();
        }
        // If the document does not have the field we're interested in or that field isn't a number AND the user did
        // not specify that we should increment on updates, then just continue the save without any increment logic.
        else
            next();
    });
};
  1. 修改所有
    require()
    ,取决于
    mongoose-auto-increment.js
    位置。
require('../util/mongoose-auto-increment.js');
就这些了
© www.soinside.com 2019 - 2024. All rights reserved.