Javascript 树莓皮mongodb帮助连接

Javascript 树莓皮mongodb帮助连接,javascript,node.js,mongodb,mongoose,raspberry-pi,Javascript,Node.js,Mongodb,Mongoose,Raspberry Pi,所以我刚刚开始学习web开发,我一直在关注colt steele在udemy上的课程。这门课正在发展壮大,我正在尝试在raspberry pi 4上托管我自己的网站(我有32位版本和新的64位版本)。我的win10笔记本电脑上的web应用程序和数据库在本地运行良好,但我无法让应用程序在我的pi上运行。我使用的是节点v12.18.3、express v4.17.1和mongodb v4.4(shell和服务器、win10机器)以及mongodb v2.4.14(raspberry pi) 我似乎遇

所以我刚刚开始学习web开发,我一直在关注colt steele在udemy上的课程。这门课正在发展壮大,我正在尝试在raspberry pi 4上托管我自己的网站(我有32位版本和新的64位版本)。我的win10笔记本电脑上的web应用程序和数据库在本地运行良好,但我无法让应用程序在我的pi上运行。我使用的是节点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,请将mongo版本更新为=>3.1.0并使用此连接代码:

MongoClient.connect("mongodb://localhost:27017/Fries-and-ketchup", {
   useNewUrlParser: true,
   useUnifiedTopology: true
 })
或者甚至使用mongoose驱动程序,如果您想保留实际的MongoDB版本(2.x.x),请尝试如下连接:

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

由于MongoClient是一个类,因此需要使用
new

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 });