简体   繁体   中英

error : .find(…) is not a function nodejs mongoose

I define a controller which code like bellow:

const  paymentModel = require("../../models/University");
class PaymentController {
async getPaymentList(req , res){
   const list = await paymentModel.find() 
   .select("amount  description  payDate payId" ).limit(20);
   res.send(list);
}

router like bellow:

const router = require ("express").Router();
const controller = require("../http/controller/PaymentController");
router.get('/' ,controller.getPaymentList);
module.exports= router;

model:

 const mongoose = require ("mongoose");
 const schemaPay = new mongoose.Schema({
 payId  :{type:Number, required: true},
 amount :{type :String , required: true},
 description :{type :String , required:true},
 courseId:[schemaCourse] ,
 studentId:[schemaStudent],
 payDate :String ,
 });
 const paymentModel= mongoose.model('StudentPayments' , schemaPay); 
 module.exports={paymentModel };

when I test api with postman I get error:paymentModel.find is not a function can any one help me where is the problem?

For your model, you are exporting an object with the key paymentModel in it.

In order to access paymentModel from inside that object, you would have to do one of the following:

const  {paymentModel} = require("../../models/University");

or

const  UniversityModel = require("../../models/University"); //then access paymentModel like UniversityModel.paymentModel

You could also just export only one particular model instead if you don't want to have to do this, but you say you have multiple exports so yeah.

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