mongoose的connect是特定于版本的吗?

ocebsuys  于 2023-06-06  发布在  Go
关注(0)|答案(1)|浏览(189)

mongoose.connect部分给我带来了问题。如果我不包含useNewUrlParser:true,使用UnifiedTopology:true,在终端中运行app.js会给予我关于包含这些部分的DeprecationWarning。但是,如果我添加了它们,运行app.js什么也不做。它永远冻结了。

//jshint esversion:6

const mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/", {
    useNewUrlParser: true, useUnifiedTopology: true
});

//Schema is scaffolding of date. For instance here's one for fruits. 
const fruitSchema = new mongoose.Schema({
    name: String,
    rating: Number,
    review: String
});

//This part is also required for upper one to work. 
const Fruit = mongoose.model("Fruit", fruitSchema);

const fruit = new Fruit({
    name: "Apple",
    rating: 7,
    review: "I loved it."
});

fruit.save();
yx2lnoni

yx2lnoni1#

mongoose.connect("mongodb://localhost:27017/fruitsDB", {
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(() => console.log('DB Connected!'))
.catch(err => {
console.log("DB Connection Error: ${err.message}");
});

相关问题