如果以这种方式连接到数据库,为什么会出现此错误?
这是我的代码:
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Player = require('../models/player'); //import player database
const db = mongoose.connect('mongodb://user:pass@ds263493.mlab.com:63493/user', { useNewUrlParser: true });
mongoose.Promise = global.Promise; //Promise type avoids warnings mongoose throws
mongoose.connect(db, function(err)
{
if(err){
console.error("Error! "+ err);
}
});
router.get('/players', function(req, res) //make req to players
{
console.log('Get request for all players');
Player.find({}) //connected to players collection in database
.exec(function(err, players) //once players are found, send as response to browser
{
if(err)
{
console.log("Error retrieving players");
}
else
{
res.json(players);
}
})
});
module.exports = router;
我将数据库的用户名和密码更改为默认值,仅用于此问题,这不是问题。
以某种方式说mongoose.connect(“”)的参数不是字符串时,我什至尝试过useNewUrlParser:true,但它仍然给我同样的错误。