微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

树莓派mongodb帮助连接

如何解决树莓派mongodb帮助连接

因此,我只是专心于Web开发,正如我一直在跟着Colt Steele的udemy课程一样。上课的人数激增,我正在尝试在树莓派4(我拥有32位版本和新的64位版本)上托管自己的网站。我的win10笔记本电脑上的Web应用程序和数据库可以在本地正常运行,但是无法在pi上运行该应用程序。我正在使用Node v12.18.3,express v4.17.1和mongodb v4.4(shell和服务器,win10计算机)和mongodb v2.4.14(raspBerry pi)。

我似乎遇到的问题是连接到mongo数据库。我认为这是因为我正在使用mongoose尝试连接到mongodb,但是mongoose不会支持mongodb版本2.4.14。

这是我可以在Win 10机器上连接的代码

mongoose.connect('mongodb://localhost:27017/Fries-and-Ketchup',{ useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false });
mongoose.connection.on('connected',() => {
    console.log('connected to mongoDB');
});
mongoose.connection.on('error',err => {
    console.log('error connecting to mongodb');
});

这是我在终端中遇到的错误

(node:31771) UnhandledPromiseRejectionWarning: MongoServerSelectionError: Server at localhost:27017 reports maximum wire version 0,but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)
    at Timeout._onTimeout (/home/pi/Fries_and_ketchup/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
    at listOnTimeout (internal/timers.js:549:17)
    at processtimers (internal/timers.js:492:7)
(node:31771) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:31771) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

所以我尝试使用发现的另一段代码,但不幸的是,这也不起作用:

const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://10.0.0.109:27017";

MongoClient.connect("mongodb://localhost:27017/Fries-and-ketchup",{
   useNewUrlParser: true,useUnifiedTopology: true
 })

如何连接到Pi上的Mongo数据库

解决方法

您正在严格执行此操作,但请使用:

检查您的mongodb版本
mongo --version

如果此版本为 3.1.0并使用以下连接代码:

MongoClient.connect("mongodb://localhost:27017/Fries-and-ketchup",{
   useNewUrlParser: true,useUnifiedTopology: true
 })

甚至使用mongoose驱动程序,如果要保留实际的mongo数据库版本(2.x.x),请尝试按以下方式连接:

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/Fries-and-ketchup',function(err,db) {console.log("connect",db)});
,

尝试

const {MongoClient} = require('mongodb');
const url = "mongodb://127.0.0.1:27017/Fries-and-ketchup";

const client = new MongoClient.connect(url,{ useNewUrlParser: true,useUnifiedTopology: true });

由于MongoClient是一个类,因此您需要使用new声明它

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。