MongoDB Atlas error: invalid schema, expected mongodb
MongoDB Atlas error: invalid schema, expected mongodb
我 运行 通过 GCP Hello World app tutorial。我卡在了 server.js 这一步。
server.js的代码如下:
'use strict';
const mongodb = require('mongodb');
const http = require('http');
const nconf = require('nconf');
let uri = 'mongodb+srv://my_name:<mypassword>@mydatabase-clr75.gcp.mongodb.net/test?retryWrites=true&w=majority';
if (nconf.get('mongoDatabase')) {
uri = `${uri}/${nconf.get('mongoDatabase')}`;
}
console.log(uri);
mongodb.MongoClient.connect(uri, (err, db) => {
if (err) {
throw err;
}
// Create a simple little server.
http.createServer((req, res) => {
if (req.url === '/_ah/health') {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write('OK');
res.end();
return;
}
const collection = db.collection('Messages');
var datetime = new Date();
const msg = {
msgDescription: '\nHello World received on ' + datetime
};
collection.insert(msg, (err) => {
if (err) {
throw err;
}
// push out a range
let msglist = '';
collection.find().toArray((err, data) => {
if (err) {
throw err;
}
data.forEach((msg) => {
msglist += `${msg.msgDescription}; `;
});
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write('Messages received so far:\n');
res.end(msglist);
});
});
}).listen(process.env.PORT || 8080, () => {
console.log('started web process');
});
});
我收到如下错误:
mongodb+srv://my_name:@mydatabase-clr75.gcp.mongodb.net/test?retryWrites=true&w=majority
/home/herboratory/node_modules/mongodb/lib/url_parser.js:19
throw new Error('invalid schema, expected mongodb');
^ Error: invalid schema, expected mongodb
at module.exports (/home/herboratory/node_modules/mongodb/lib/url_parser.js:19:11)
at connect (/home/herboratory/node_modules/mongodb/lib/mongo_client.js:486:16)
at Function.MongoClient.connect (/home/herboratory/node_modules/mongodb/lib/mongo_client.js:250:3)
at Object. (/home/herboratory/server.js:12:21)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test@1.0.0 start: node
server.js
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the
test@1.0.0 start script. npm ERR! This is probably not a problem with
npm. There is likely additional logging output above. npm ERR! A
complete log of this run can be found in: npm ERR!
/home/herboratory/.npm/_logs/2019-06-26T03_58_26_823Z-debug.log
我想知道这应该是格式错误,在阅读了这里有相同错误行的其他帖子后,所以我尝试了 '...', "..." 并且没有任何引号但仍然存在错误。请指导我错误在哪里?
代码里面除了URI还有其他地方需要修改吗?据我所知,我只需要插入我自己的 Atlas Connection 字符串。
非常感谢。
错误 invalid schema, expected mongodb
表示您使用的是过时的节点驱动程序版本。旧驱动程序无法解析新的 mongodb+srv
URI 方案。
在该票证的节点驱动程序版本 3.0 中添加了对 mongodb+srv
方案的支持:NODE-1145。
使用以下方式升级您的节点驱动程序:
$ npm install mongodb
错误应该会消失。
我有同样的错误。问题出在 mongoDB Atlas 中的设置和我的应用程序中的设置。
在mongoDB图集中:
- 创建数据库和集合
- 创建数据库用户
- 在 IP 白名单、网络访问中添加您的 IP 地址 (public)
我的解决方案示例:
文件 .env
MONGO_URI=mongodb+srv://jmendoza:your-password@cluster0-7rxkw.mongodb.net/nodeapi?retryWrites=true&w=majority
PORT=3000
文件app.js
const express = require('express');
const morgan = require('morgan');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const { postRoutes } = require('./routes/posts');
const app = express();
const port = process.env.PORT || 3000;
dotenv.config();
// BD
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('mongoDB, Atlas. Connected'))
.catch((err) => console.error(err));
// Middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(expressValidator());
// Routes
app.use('/api/v1', postRoutes);
app.listen(port, () => {
console.log(`A NodeJS API is listining on port: ${port}`);
});
文件package.json
{
"name": "node-api",
"version": "1.0.0",
"description": "A NodeJS API",
"main": "app.js",
"scripts": {
"dev": "nodemon app.js"
},
"keywords": [
"node",
"api"
],
"author": "Jonathan Mendoza",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^5.3.1",
"mongoose": "^5.9.7",
"morgan": "^1.9.1",
"nodemon": "^2.0.3"
}
}
运行 应用程序(控制台)
jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Basic-Node-API$ npm run dev
> node-api@1.0.0 dev /home/jmendoza/IdeaProjects/NodeJS-API-Course/Basic-Node-API
> nodemon app.js
[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
A NodeJS API is listining on port: 3000
mongoDB, Atlas. Connected
NodeJS 版本
jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Basic-Node-API$ node -v
v13.12.0
你可以在GitHub上看到我的完整代码:
https://github.com/JonathanM2ndoza/NodeJS-API-Course/tree/master/Basic-Node-API
我 运行 通过 GCP Hello World app tutorial。我卡在了 server.js 这一步。
server.js的代码如下:
'use strict';
const mongodb = require('mongodb');
const http = require('http');
const nconf = require('nconf');
let uri = 'mongodb+srv://my_name:<mypassword>@mydatabase-clr75.gcp.mongodb.net/test?retryWrites=true&w=majority';
if (nconf.get('mongoDatabase')) {
uri = `${uri}/${nconf.get('mongoDatabase')}`;
}
console.log(uri);
mongodb.MongoClient.connect(uri, (err, db) => {
if (err) {
throw err;
}
// Create a simple little server.
http.createServer((req, res) => {
if (req.url === '/_ah/health') {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write('OK');
res.end();
return;
}
const collection = db.collection('Messages');
var datetime = new Date();
const msg = {
msgDescription: '\nHello World received on ' + datetime
};
collection.insert(msg, (err) => {
if (err) {
throw err;
}
// push out a range
let msglist = '';
collection.find().toArray((err, data) => {
if (err) {
throw err;
}
data.forEach((msg) => {
msglist += `${msg.msgDescription}; `;
});
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write('Messages received so far:\n');
res.end(msglist);
});
});
}).listen(process.env.PORT || 8080, () => {
console.log('started web process');
});
});
我收到如下错误:
mongodb+srv://my_name:@mydatabase-clr75.gcp.mongodb.net/test?retryWrites=true&w=majority /home/herboratory/node_modules/mongodb/lib/url_parser.js:19 throw new Error('invalid schema, expected mongodb'); ^ Error: invalid schema, expected mongodb at module.exports (/home/herboratory/node_modules/mongodb/lib/url_parser.js:19:11) at connect (/home/herboratory/node_modules/mongodb/lib/mongo_client.js:486:16) at Function.MongoClient.connect (/home/herboratory/node_modules/mongodb/lib/mongo_client.js:250:3) at Object. (/home/herboratory/server.js:12:21) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Function.Module.runMain (module.js:694:10) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test@1.0.0 start:
node server.js
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the test@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR!
/home/herboratory/.npm/_logs/2019-06-26T03_58_26_823Z-debug.log
我想知道这应该是格式错误,在阅读了这里有相同错误行的其他帖子后,所以我尝试了 '...', "..." 并且没有任何引号但仍然存在错误。请指导我错误在哪里?
代码里面除了URI还有其他地方需要修改吗?据我所知,我只需要插入我自己的 Atlas Connection 字符串。
非常感谢。
错误 invalid schema, expected mongodb
表示您使用的是过时的节点驱动程序版本。旧驱动程序无法解析新的 mongodb+srv
URI 方案。
在该票证的节点驱动程序版本 3.0 中添加了对 mongodb+srv
方案的支持:NODE-1145。
使用以下方式升级您的节点驱动程序:
$ npm install mongodb
错误应该会消失。
我有同样的错误。问题出在 mongoDB Atlas 中的设置和我的应用程序中的设置。
在mongoDB图集中:
- 创建数据库和集合
- 创建数据库用户
- 在 IP 白名单、网络访问中添加您的 IP 地址 (public)
我的解决方案示例:
文件 .env
MONGO_URI=mongodb+srv://jmendoza:your-password@cluster0-7rxkw.mongodb.net/nodeapi?retryWrites=true&w=majority
PORT=3000
文件app.js
const express = require('express');
const morgan = require('morgan');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const { postRoutes } = require('./routes/posts');
const app = express();
const port = process.env.PORT || 3000;
dotenv.config();
// BD
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('mongoDB, Atlas. Connected'))
.catch((err) => console.error(err));
// Middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(expressValidator());
// Routes
app.use('/api/v1', postRoutes);
app.listen(port, () => {
console.log(`A NodeJS API is listining on port: ${port}`);
});
文件package.json
{
"name": "node-api",
"version": "1.0.0",
"description": "A NodeJS API",
"main": "app.js",
"scripts": {
"dev": "nodemon app.js"
},
"keywords": [
"node",
"api"
],
"author": "Jonathan Mendoza",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^5.3.1",
"mongoose": "^5.9.7",
"morgan": "^1.9.1",
"nodemon": "^2.0.3"
}
}
运行 应用程序(控制台)
jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Basic-Node-API$ npm run dev
> node-api@1.0.0 dev /home/jmendoza/IdeaProjects/NodeJS-API-Course/Basic-Node-API
> nodemon app.js
[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
A NodeJS API is listining on port: 3000
mongoDB, Atlas. Connected
NodeJS 版本
jmendoza@jmendoza-ThinkPad-T420:~/IdeaProjects/NodeJS-API-Course/Basic-Node-API$ node -v
v13.12.0
你可以在GitHub上看到我的完整代码:
https://github.com/JonathanM2ndoza/NodeJS-API-Course/tree/master/Basic-Node-API