mongoose 电子邮件模型一经编译就无法覆盖

bfrts1fy  于 2023-01-31  发布在  Go
关注(0)|答案(1)|浏览(169)
// 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

tzcvj98z

tzcvj98z1#

我相信你们这里有Email型号

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

用空模式声明集合会导致mongodb覆盖现有的模式。您可以直接使用Email.aggregate(...)

相关问题