TypeError:Model.findById不是函数(罕见错误)

时间:2017-07-12 05:15:42

标签: node.js rest api express mongoose

这种情况很少发生,但却很紧张。

我得到了这个Schema和Model:

//Product Schema
let ProductSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  price: {
    type: Number,
    required: true
  },
  inStock: {
    type: Number,
    required: true
  },
  barCode: {
    type: String,
    required: true,
    index: true,
    unique: true
  }
}, {
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  }
});

并像这样导出: module.exports = v1_connection.model('Product', ProductSchema);

所以,在同一个文件夹但是另一个文件中,我得到了这两个Schema(第一个嵌入在第二个中):

/*
  Product in Sale Schema
*/
let InSaleProductSchema = new Schema({
  product: {
    type: Schema.ObjectId,
    ref: 'Product',
    unique: true
  },
  qty: {
    type: Number,
  },
  total: {
    type: Number,
  },
  inSale: {
    type: Boolean
  }
}, {
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  }
});

/*
  Sale Schema
*/
let SaleSchema = new Schema({
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  products: [InSaleProductSchema],
  total: {
    type: Number,
  },
  isComplete: {
    type: Boolean,
    default: false
  }
}, {
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  }
});

嗯,它在预先保存的问题中指出的问题是'需要从产品保存文档中减去所需数量的钩子。在这一行中它是问题

// Create and execute the query to find the product by de product ID
let query_product = Product.findById(product_id);

总是,当我想要运行钩子时,请给我一个错误:

TypeError: Product.findById is not a function

因此。 。 。什么错了?

**如果您询问我如何导入产品型号:const Product = require('./product'); **

1 个答案:

答案 0 :(得分:-1)

您应该为每个模式保留单独的文件:

<强> ProductSchema.js:

//Product Schema
let ProductSchema = new Schema({
  name: {    type: String,    required: true  },
  description: {    type: String  },
  price: {    type: Number,    required: true  },
  inStock: {    type: Number,    required: true  },
  barCode: {    type: String,    required: true,    index: true,    unique: true  }
 }, { timestamps: true });

// creating model
module.exports = mongoose.model('ProductSchema', ProductSchema);

<强> InSaleProductSchema.js:

/*
  Product in Sale Schema
*/
let InSaleProductSchema = new Schema({
    product: { type: Schema.ObjectId, ref: 'Product', unique: true },
    qty: { type: Number, },
    total: { type: Number, },
    inSale: { type: Boolean }
}, {
        timestamps: true
    });

module.exports = mongoose.model('InSaleProductSchema', InSaleProductSchema);

<强> SaleSchema.js:

/*
  Sale Schema
*/
let SaleSchema = new Schema({
    user: { type: Schema.ObjectId, ref: 'User' },
    products: [InSaleProductSchema],
    total: { type: Number, },
    isComplete: { type: Boolean, default: false }
}, {
        timestamps: true
    });
module.exports = mongoose.model('SaleSchema', SaleSchema);

这里是您的索引文件:

<强> index.js:

const Product = require('./product');
// Create and execute the query to find the product by de product ID
let query_product = Product.findById(product_id).exec();
console.log(query_product);

希望上面的代码适合你: