Home > other >  what is TypeError: Cannot read properties of undefined (reading 'Promise')
what is TypeError: Cannot read properties of undefined (reading 'Promise')

Time:08-18

const express = require("express");
require("./DB/conn");
const MensRankings = require("./models/runners");

const app = express();
app.use(express.json());

app.post('/mens' ,async(req,res)=>{
    console.log(req.body);
    try {
        const addMensRecord = new MensRankings(req.body);
        const data = await addMensRecord.save()
        console.log(data);
    }
    catch (error) {
        console.log(error)
    }
})


app.listen(8000, ()=>{
    console.log("Listening to port 8000");
})

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/mens100')
.then(()=>{
    console.log("Connection is done");
})
.catch((e)=>{
    console.log("error");
});
const express = require('express');
const mongoose = require('mongoose');
runnersSchema = new mongoose.Schema({
    ranking : Number,
    name : String,
    dob : Number,
    score :Number,
    country : String,
    }
);

const MensRankings = new mongoose.Collection("Men", runnersSchema);

module.exports = MensRankings;

how to resolve this and what's the problem?

PS C:\Users\Sohel\Desktop\mens100> node app.js
C:\Users\Sohel\Desktop\mens100\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:26 this.Promise = options.Promise || Promise; ^

TypeError: Cannot read properties of undefined (reading 'Promise') at new NativeCollection (C:\Users\Sohel\Desktop\mens100\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:26:26) at Object. (C:\Users\Sohel\Desktop\mens100\models\runners.js:13:22) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (C:\Users\Sohel\Desktop\mens100\app.js:3:22) at Module._compile (node:internal/modules/cjs/loader:1105:14)

CodePudding user response:

Can you please change

const addMensRecord = new MensRankings(req.body);
const data = await addMensRecord.save()
console.log(data);

to

const data = await MensRankings.create(req.body);
console.log(data);

and check if it is working.

Ah see it..

const MensRankings = new mongoose.Collection("Men", runnersSchema);

needs to be

const MensRankings = mongoose.model("Men", runnersSchema);

  • Related