类型错误:无法将对象"#"分配给只读属性"插件<Mongoose>"



我目前正在学习 Angular 并完成教程 T他的指南将引导您创建一个基本的 MEAN 应用程序https://navakos.slab.com/public/building-a-mean-application-c9369d11?utm_source=mybridge&utm_medium=blog&utm_campaign=read_more 本文使用 Babel 6,我使用的是 babel 7。我已经能够更新.但是现在收到错误

$ npm run dev
> backend@1.0.0 dev xxxMEAN_Applicationbackend
> babel-watch server.js

xxxbackendnode_modulesmongooselibconnection.js:64
this.plugins = [];
^
TypeError: Cannot assign to read only property 'plugins' of object '#<Mongoose>'
at Mongoose.Connection (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendnode_modulesmongooselibconnection.js:64:16)
at Mongoose.NativeConnection [as Connection] (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendnode_modulesmongooselibdriversnode-mongodb-nativeconnection.js:18:22)
at Object.<anonymous> (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendserver.js:15:10)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at babelWatchLoader (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendnode_modulesbabel-watchrunner.js:51:13)
at Object.require.extensions.<computed> [as .js] (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendnode_modulesbabel-watchrunner.js:62:7)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at process.<anonymous> (C:UsersnnamachaDocumentsGitHubMEAN_Applicationbackendnode_modulesbabel-watchrunner.js:110:21)
at process.emit (events.js:223:5)
at emit (internal/child_process.js:876:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21)

我的包.杰森

{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node server.js",
"dev": "babel-watch server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.8.3",
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"babel-watch": "^7.0.0"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.8.9"
}
}

下面是我的服务器.js

服务器.js

import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import mongoose, { connection } from 'mongoose';
const app = express();
const router = express.Router();
app.use(cors());
app.use(bodyParser.json());
// Connects to the MongoDB database collection.
mongoose.Connection('');
const Connection = mongoose.Connection;
connection.once('open',() => {
console.log('MongoDB database connection established successfully!')
});
app.use('/',router);

// Establishes which port the backend runs on.
app.listen(4000, () => console.log('Express server running on port 4000'));

我可能错过了什么?

我和你有同样的错误消息!我认为您的第一个问题是您实际上没有连接到数据库,请尝试以下操作:

mongoose.connect('mongodb://127.0.0.1:27017/', {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true});
var connection = mongoose.connection;

让我知道这是否有帮助!

最新更新