Home > other >  node js pass paramater to mongoose model
node js pass paramater to mongoose model

Time:04-09

I would like to pass a parameter from my route to my model. This is what I have in routes/data.js:

const express = require('express');
const router = express.Router();
const Data = require('../models/data')
const mongoose = require('mongoose');

router.get('/', async (req, res) => {
  Data("object").find().where('dos').ne([])
  .exec((err, data) => {
    res.send(data);
  });
});

module.exports = router; 

model/data.js:

const mongoose = require('mongoose');

const Data = (doc) => mongoose.model(doc,new mongoose.Schema({}), doc);

exports.Data = Data;

The code compliles but when I run it I got this error: (node:25344) UnhandledPromiseRejectionWarning: TypeError: Data is not a function at C:\Users\project\routes\data.js:7:3

CodePudding user response:

try doing this:

const { Data } = require('../models/data')

because you have exported the function as exports.Data = Data; not like module.exports = Data, so this wont work as const Data = require('../models/data').

  • Related