【问题标题】:Cannot connect cluster mongo.db atlas无法连接集群 mongo.db 图集
【发布时间】:2019-10-17 03:03:52
【问题描述】:

我目前正在尝试学习 MEAN 堆栈。本教程是关于任务管理器的。现在我正在尝试连接到 mongodb atlas 以检索数据库中的示例数据,但它们没有显示 http://localhost:3000/api/tasks

这是我的 tasks.js 文件

var express = require('express');
var router = express.Router();
var mongojs = require('mongojs');
var db = mongojs('drivers',['tasks']);


router.get('/tasks', function(req, res, next){
    db.tasks.find(function(err, tasks){
        if(err){
            res.send(err);
        }
        res.json(tasks);
    });
});

module.exports = router;

这是我的 server.js 文件

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var task = require('./routes/tasks');
var app = express();
var port = 3000;

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.use(express.static(path.join(__dirname, 'client')));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use('/', index);
app.use('/api', task);

app.listen(port, function(){
    console.log('Server started on port' +port);
});

这是我的 package.json 文件

{
  "name": "mytasklist",
  "version": "1.0.0",
  "description": "Task Manager",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "ejs": "^2.6.1",
    "express": "^4.17.1",
    "mongojs": "^2.6.0"
  }
}

【问题讨论】:

标签: node.js mongodb mongojs


【解决方案1】:

需要使用mongojs 3.0.0版本连接atlas

【讨论】:

    【解决方案2】:

    问题解决了,我为我的 nodejs 驱动程序粘贴了错误的版本。

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 2019-06-10
      • 2020-02-08
      • 2020-04-28
      • 2018-04-22
      • 2021-09-18
      • 2020-07-12
      • 2015-10-27
      相关资源
      最近更新 更多