node.js - 如何将 Node API 连接到本地计算机上的特定 mongoDB 数据库/集合

标签 node.js mongodb

由于问题的级别有多高,以前可能已经有人问过这个问题,但是我找不到解决方案,并且正在努力进行设置。我正在使用 MERN 堆栈开发我的第一个全堆栈 Web 应用程序。我在 Mac 上。我在这里所做的一切都在我的本地计算机上。

对于 MongoDB,我已将其安装在本地计算机上。我正在运行 mongod dameom。这是我的交互式 mongo shell 中的内容:

// run in terminal
> mongo

> show dbs
admin              0.000GB
config             0.000GB
mydboverhere       0.064GB
local              0.000GB

> use mydboverhere
switched to db mydboverhere

> show collections     
table_one
table_two
andathirdtable

我想将我的 Node/express API 连接到mydboverhere 数据库。在我的 Node 目录结构中,我有一个 models 目录,其中包含:

/models/index.js

var mongoose = require('mongoose');
mongoose.set('debug', true);

// is this line correct? 
mongoose.connect('mongodb://localhost:27017/mydboverhere/table_one');

mongoose.Promise = Promise;
module.exports.Todo = require("./table1"); // this requires table1.js

/models/table1.js

// this matches the form of the data in the database, I believe 
var mongoose = require('mongoose');
var tab1Schema = new mongoose.Schema({
    name: { 
        type: String,
        required: 'cannot be blank'
    },
    completed: {
        type: Boolean,
        default: false
    },
    created_date: {
        type: Date,
        default: Date.now
    }
})

var Table1 = mongoose.model('Table1', tab1Schema)
module.exports = Table1;

我相信我的 /routes/tableroutes 文件正确:

var express = require('express');
var router = express.Router();
var db = require('../models')

router.get('/', function(req, res){
    // res.send("Hello from the table1 route")
    db.Table1.find()
        .then(function(data) {
            res.json(data);
        })
        .catch(function(err) {
            res.send(err);
        })
});

module.exports = router;

而且我认为我也正确地将这些路由加载到我的 root/index.js 文件中:

var express = require('express')
var app = express();

var tableRoutes = require('./routes/tableroutes');

// test the root route
app.get('/', function(req, res){
    res.send("Hello from the Root Route")
});

// set base route to /api for my tableRoutes    
app.use('/api', tableRoutes); 

app.listen(3000, () => console.log('Example app listening on port 3000!'))

不幸的是,在 mongod 运行的情况下,当我尝试运行 Node Index.js 来运行我的 Node 应用程序时,我在命令行中收到以下错误消息:

...( Node :66245)UnhandledPromiseRejectionWarning:错误:不支持的主机“localhost:27017/mydboverhere/table_one”,主机必须经过 URL 编码,并且最多包含一个未编码的斜杠...

我现在被困在这里......几乎,我不确定我是否正确连接我的 Node API 和 mongodb。这一切都是在我的本地计算机上完成的,并且我已经将 mongodb 安装在/data/db 上,因为它应该是这样。错误可能是由于集合名称 table_one 中的下划线造成的。也许错误是因为 mongo 中的 table_one 集合中的数据与 table1.js 中的模式不完全匹配(我通过将 R 中的数据帧插入其中单独创建了 mongodb,然后编写了 table1.js 模式来匹配它)。

无论以下哪个问题是问题,我都不确定,并且我正在努力继续。非常感谢这里的任何帮助!

EDIT1:我有一种强烈的感觉,下面这行:

mongoose.connect('mongodb://localhost:27017/mydboverhere/table_one');

不正确,我看到连接到特定数据库的正确方法。

EDIT2:我认为还有另一个名为 mongoDB 的 javascript 库可以用于此目的,但我非常希望将其与 mongoose 一起使用。

最佳答案

我认为这里有一个错误:

您正在使用 thisdboverhere,而它应该是 mydboverhere

mongoose.connect('mongodb://localhost:27017/mydboverhere', function(){
 // do your process here
});

或者

 mongoose.connect('mongodb://localhost:27017/mydboverhere');
  var db = mongoose.connection // I think you are forgetting to instantiate the connection here

关于node.js - 如何将 Node API 连接到本地计算机上的特定 mongoDB 数据库/集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49372419/

相关文章:

mongodb - PyMongo:当 no_cursor_timeout=True 时光标会发生什么

eclipse - 使用 Eclipse 远程开发 Node.js 应用程序

javascript - 为什么反序列化我的目标数组的序列化副本可以修复内存泄漏问题?

javascript - 使用node.js实现串口实时数据采集

node.js - 如何保存具有特定 id 的图像并通过 mongo 数据库中的该 id 从数据库中获取该图像。感谢您的任何想法或建议

java - 当从 java 程序在 mongodb 中存储 long 值时,该值以这种格式存储 NumberLong(n)

node.js - 未找到 FFMPEG Discord.js

node.js - 从服务器更新客户端信息,无需每次发送请求

node.js - Mongoose 聚合查询跳过 getter

node.js - Heroku 错误 : Invalid schema, 预期为 `mongodb` 或 `mongodb+srv`