开发者问题收集

什么是 TypeError:无法读取未定义的属性(读取“Promise”)

2022-08-18
1078
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;

如何解决这个问题,问题是什么?

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:无法读取未定义的属性(读取“Promise”) 在新的 NativeCollection(C:\Users\Sohel\Desktop\mens100\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:26:26) 在 Object。 (C:\Users\Sohel\Desktop\mens100\models\runners.js:13:22) 在 Module._compile (node:internal/modules/cjs/loader:1105:14) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10) 在 Module.load (node:internal/modules/cjs/loader:981:32) 在 Function.Module._load (node:internal/modules/cjs/loader:822:12) 在 Module.require (node:internal/modules/cjs/loader:1005:19) 在 require (node:internal/modules/cjs/helpers:102:18) 在 Object. (C:\Users\Sohel\Desktop\mens100\app.js:3:22) 位于 Module._compile (node:internal/modules/cjs/loader:1105:14)

1个回答

您能否将

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

更改为

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

并检查它是否正常工作。

啊,看到了。。

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

需要是

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

ratzownal
2022-08-18