简体   繁体   中英

Model.findById is not a function

In main.js I invoke driver.claimDropoff(myOrderId) which looks like

const utils = require('../utils');
...
async claimDropoff(order) {
        order = await utils.ensureOrder(order);
        order.status = 1;
        order.dropoffDriver = this._id;
        await order.save()
        return this
    }

utils.js

const Order = require('./types/order');
...
async function ensureOrder(d) {
    d = (d.hasOwnProperty('_id')) ? d : await Order.findById(d);
    if (typeof d === 'undefined') { return new Error('Order not found') }
    return d;
}

types/order.js

...
const Driver = mongoose.model('Driver', DriverSchema, 'drivers');
module.exports = Driver;

My directory tree looks like this:

main.js
utils.js
|
└───types
    driver.js
    order.js

Yet I get the error UnhandledPromiseRejectionWarning: TypeError: Order.findById is not a function , which should not happen! I am requiring a mongoose.Model which should have a function findById(id: any, callback: function)

What causes this strange behaviour? Intellisense says that this is perfectly legal.

You should destructure the Order object. Use it like this.

const { Order }= require('./types/order');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM