【问题标题】:Unable to connect to mongodb with Node.js无法使用 Node.js 连接到 mongodb
【发布时间】:2015-11-23 00:45:16
【问题描述】:

我正在关注 edx 的 MEAN 堆栈课程并尝试进行设置。这是我要运行的 index.js 文件:

var mongodb = require('mongodb');

var uri = 'mongodb://localhost:27017/example';
mongodb.MongoClient.connect(uri, function(error, db){
    if (error) {
        console.log(error);
        process.exit(1);
    }

    db.collection('sample').insert({x:1}, function(error, result){
        if(error){
            console.log(error);
            process.exit(1);
        }

        db.collection('sample').find().toArray(function(error, docs){
            if (error) {
                console.log(error);
                process.exit(1);
            }
        });
    });
});

当我在终端中执行node index.js 时,出现错误“地址已用于套接字:0.0.0.0:27017”,感谢 Sacha 回答的 unable to start mongodb local server 这篇帖子(答案4)。 然后我得到错误“找不到模块'mongodb'”,通过全局安装mongodb并在我的应用程序文件夹上使用npm link mongodb解决了这个问题。现在我没有收到任何错误,而是我没有得到任何东西。我猜只是一个等待答案的光标。我使用 OS X El Capitan。

【问题讨论】:

    标签: javascript node.js mongodb mean-stack edx


    【解决方案1】:

    这段代码除了在一个名为 sample 的集合中插入一个文档之外什么也不做。你需要使用Mongo客户端检查它的结果,否则它看起来没用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2016-04-14
      • 2019-10-17
      • 2013-11-02
      • 1970-01-01
      • 2021-04-01
      • 2015-01-28
      相关资源
      最近更新 更多