【发布时间】:2020-09-09 14:15:00
【问题描述】:
所以我刚刚开始学习网络开发,并且一直在关注 colt Steele 的 udemy 课程。课程越来越多,我正在尝试在树莓派 4 上托管我自己的网站(我有 32 位版本和新的 64 位版本)。我的网络应用程序和数据库在我的 win10 笔记本电脑上本地运行良好,但我无法让该应用程序在我的 pi 上运行。我正在使用 Node v12.18.3、express v4.17.1 和 mongodb v4.4(shell 和服务器,win10 机器)和 mongodb v2.4.14(树莓派)。
我似乎遇到的问题是连接到 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 数据库?
【问题讨论】:
标签: javascript node.js mongodb mongoose raspberry-pi