简体   繁体   中英

Cannot overwrite `emails` model once compiled

// Require mongoose module
const mongoose = require("mongoose");
// Importing express module
const express = require('express');
const jwt = require('jsonwebtoken'); 
const router = express.Router();
const dotenv = require('dotenv'); 
dotenv.config();
let cors = require("cors");
router.use(cors());
router.use(express.json()); 
// Set Up the Database connection
const { Email } = require( '../../models/Stats' );
const M_URL = process.env.MONGO_URL;
mongoose.connect(M_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
const schema = new mongoose.Schema({});


router.get('/',(req,res)=>{
    const collection= mongoose.model('emails',{}) || mongoose.model.emails;
    collection.aggregate([
      {
    
        "$group": {
          "_id": {
            "id": {
              "$dateToString": {
                "date": "$createdAt",
                "format": "%Y-%m-%d"
              }
            }
          },
          "iEmailCount_Success": {
            "$sum": {
              "$cond": {
                "if": {
                  "$eq": [
                    "$sEmailStatus",
                    "Sent"
                  ]
                },
                "then": 1,
                "else": 0
              }
            }
          },
          "iEmailCount_Failded": {
            "$sum": {
              "$cond": {
                "if": {
                  "$eq": [
                    "$sEmailStatus",
                    "Failed"
                  ]
                },
                "then": 1,
                "else": 0
              }
            }
          }
        }
      },
      {
        "$project": {
          "_id": 0,
          "cDate": "$_id.id",
          "iEmailCount_Success": 1,
          "iEmailCount_Failded": 1
        }
      } 
      ,
      {
        "$sort":{
          "cDate":1
        }
      }
    ]).exec((error,result)=>{
      if(error){
        //console.log(error);
        res.status(400).send({"error": "true","StatusCode": 400,"data":error});
      }else{
        //console.log(result);
        res.status(200).send({"error": "false","StatusCode": 200,"data":result});
      } 
    })
  });
  

module.exports = router;

this is my code and i am getting this error

Cannot overwrite emails model once compiled.

after i hit to api

error is as follow

GET /stats/emails_sent_fails undefined Cannot overwrite emails model once compiled. [2023-01-29T20:03:29.230Z] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (internal/errors.js:322:7) at ServerResponse.setHeader (_http_outgoing.js:561:11) at ServerResponse.header (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/response.js:794:10) at ServerResponse.send (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/response.js:174:12) at ServerResponse.json (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/response.js:278:15) at /home/plus91/Desktop/medixcel-email-service/config/routes.js:30:40 at Layer.handle [as handle_request] (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:328:13) at /home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:286:9 at param (/home/plus91/Desktop /medixcel-email-service/node_modules/express/lib/router/index.js:365:14) at param (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:376:14) at Function.process_params (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:421:3) at Immediate.next (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:280:10) at Immediate._onImmediate (/home/plus91/Desktop/medixcel-email-service/node_modules/express/lib/router/index.js:646:15) at processImmediate (internal/timers.js:466:21)

output whenever someone hits on route

I believe you have Email model here

const { Email } = require( '../../models/Stats' );

Declare collection with empty schema cause mongodb override the existing one. You can use Email.aggregate(...) directly instead.

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