猫鼬填充引发“未捕获的TypeError:query.exec不是函数”错误

时间:2019-05-17 16:11:48

标签: node.js mongodb mongoose mongoose-populate

我遇到的问题是Mongoose抛出了填充函数中的.populate().exec()不是函数的随机错误。

我最初在本地开发此API时使用Mongoose v4.13.18。一切都已经工作了几个月,我使用Azure的Kubernetes服务和Azure CosmosDB(托管的mongodb)进行了设置。在实时服务器上时,我开始收到有关填充不是以下功能的错误:

TypeError: user_1.User.findOne(...).populate is not a function
    at /usr/src/app/dist/controllers/users.controller.js:307:55
    at new Promise (<anonymous>)
    at Function.UsersController.login_user (/usr/src/app/dist/controllers/users.controller.js:304:16)
    at Object.<anonymous> (/usr/src/app/dist/routes/user.routes.js:239:65)
    at step (/usr/src/app/dist/routes/user.routes.js:40:23)
    at Object.next (/usr/src/app/dist/routes/user.routes.js:21:53)
    at /usr/src/app/dist/routes/user.routes.js:15:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/src/app/dist/routes/user.routes.js:11:12)
    at /usr/src/app/dist/routes/user.routes.js:229:52
    at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/src/app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/usr/src/app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/usr/src/app/node_modules/express/lib/router/layer.js:95:5)
    at /usr/src/app/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12)

我升级到Mongoose 5.5.9来查看是否可以解决该错误,然后突然我的所有集成测试开始崩溃并显示以下错误:

Uncaught TypeError: query.exec is not a function
      at populate (node_modules/mongoose/lib/model.js:4024:11)
      at _populate (node_modules/mongoose/lib/model.js:3884:5)
      at utils.promiseOrCallback.cb (node_modules/mongoose/lib/model.js:3857:5)
      at Object.promiseOrCallback (node_modules/mongoose/lib/utils.js:248:12)
      at Function.Model.populate (node_modules/mongoose/lib/model.js:3856:16)
      at model.Query.Query._completeOne (node_modules/mongoose/lib/query.js:1995:9)
      at Immediate.Query.base.findOne.call (node_modules/mongoose/lib/query.js:2032:10)
      at Immediate.<anonymous> (node_modules/mquery/lib/utils.js:116:16)

我真的很困惑,因为这个错误似乎完全在Mongoose代码库中。当我收到该错误时,我只是在执行一个非常普通的查询,如您在此处看到的那样:

Mutation.findOne({ carrier: my_carrier_id, model: "Carrier" }, (err, mutation) => {
            expect(err).to.be.null;
            expect(mutation).to.not.be.null;
            expect(mutation).to.have.property('model', 'Carrier');
            expect(mutation).to.have.property('change', `Created new carrier called ${new_test_carrier.name}`);
            expect(mutation).to.have.property('operation', 'create');

            done();
        });

Mutation模型是导出的mongoose.model()调用,并且已经工作了相当长的时间,但是直到升级Mongoose时才炸毁。奇怪的是,当我降级到v4.13.18时,同样的错误不断发生。

import * as mongoose from "mongoose";
import * as immutable from "mongoose-immutable";
import * as autopopulate from "mongoose-autopopulate";
const Schema = mongoose.Schema;

const MutationSchema = new Schema({
    carrier: {
        type: Schema.Types.ObjectId,
        ref: 'Carrier',
        index: true,
        immutable: true,
    },
    acting_user: {
        type: Schema.Types.ObjectId,
        ref: 'User', 
        index: true,
        immutable: true,
        autopopulate: true,
    },
    model: String,
    id: String,
    change: {
        type: Schema.Types.Mixed,
    },
    operation: {
        type: String,
        enum: ['create', 'update', 'delete'],
        immutable: true,
    },
});

MutationSchema.plugin(immutable);
MutationSchema.plugin(autopopulate);

export const Mutation = mongoose.model('Mutation', MutationSchema);

然后用

查询
Mutation.findOne({ carrier: my_carrier_id, model: "Carrier" }, (err, mutation) => {
            expect(err).to.be.null;
            expect(mutation).to.not.be.null;
            expect(mutation).to.have.property('model', 'Carrier');
            expect(mutation).to.have.property('change', `Created new carrier called ${new_test_carrier.name}`);
            expect(mutation).to.have.property('operation', 'create');

            done();
        });

我希望输出调用.findOne()的回调,并在此给我一个错误,或者给我记录。没错,说query.exec不是.populate()函数中的函数。

0 个答案:

没有答案