无效架构,应为“mongodb"或“mongodb+srv" [英] Invalid schema, expected `mongodb` or `mongodb+srv`
问题描述
有一个问题,一个项目简单的 node js 应用程序,添加和获取用户的数据并保存到 mongo(使用 3.. 版本),一切正常(保存、获取等),示例是
var express = require('express');var router = express.Router();var mongo = require('mongodb').MongoClient;var objectID = require('mongodb').ObjectID;var assert = require('assert');const url = 'mongodb://localhost:27017';const dbName = 'ldex';const tblOffers = 'offers';router.post('/insert', function (req, res, next) {无序顺序 = {类型:'卖',………………最大:req.body.max,保护:req.body.protection,评论:req.body.comment,日期:新日期().toDateString()};mongo.connect(url, function (err, client) {assert.equal(null, err);const db = client.db(dbName);db.collection(tblOffers).insertOne(order, function (err, res) {assert.equal(null, err);console.log('报价已放置');客户端关闭();})});res.redirect('/交换')});
但这里是另一个应用程序,解析器,它具有相同的 mongo 连接,它从其他网站添加一些数据(当不使用 mongo 一切正常时,我收到数据很好),这是代码的一部分:
<预><代码>...让 assert = require('assert');让 mongo = require('mongodb').MongoClient;让路由器 = express.Router();const url = 'mongodb://localhost:27017/';const dbName = '陶瓷';const tblOffers = '物品';.../** 从特定组中获取项目*/函数 getGroupItems(网址,回调){请求({uri:url},函数(错误,响应,正文){让列表 = [];让 $ = cheerio.load(body);$('#tovar').find('a.cat_item_disc_a').each(function(i, elem) {list[i] = 'https://plitkazavr.ru' + $(this).attr('href');});回调(列表);});}/** 从特定链接解析一项*/函数 getItem(url) {请求({uri:url},函数(错误,响应,正文){让列表 = {};让 $ = cheerio.load(body);$('#item_border').find('> #item_prop > ul').find('li.item_list').each(function(i, elem) {list[$(this).find('.item_cell').first().text()] = $(this).find('div.item_cell.item_val').text();});list.price = (parseFloat($('#item_price').text()));list.img = ('https://plitkazavr.ru' + $('#item_img').attr('src'));list.meta = ($('#item_wrap').find('meta[itemprop="description"]').attr("content"));mongo.connect(url, function (err, client) {assert.equal(null, err);const db = client.db(dbName);db.collection(tblOffers).insertOne(order, function (err, res) {assert.equal(null, err);console.log('报价已放置');客户端关闭();})});});}router.get('/parse', function (req, res, next) {getGroupItems('https://plitkazavr.ru/Naxos/Clio', 函数(物品){items.forEach(function(item, i, arr) {设置超时(函数(){获取项目(项目);}, 1000);});});res.end('ok');});它的连接中断并给出
`错误:无效架构,应为`mongodb` 或`mongodb+srv
不明白问题出在哪里,请帮助...
您的 MongoDB URL 似乎不完整,它应该包含您的 dbName
,然后才能像这样传入 mongoose 连接
const url = 'mongodb://localhost:27017/';const fullUrl = url + dbName;//应该评估为这个 'mongodb://localhost:27017/ceramo'mongo.connect(fullUrl, function (err, client) {...}
have a problem, one project simple node js app with adding and getting data from user and saving into mongo (use 3.. ver), everything OK (saving, getting etc), example is
var express = require('express');
var router = express.Router();
var mongo = require('mongodb').MongoClient;
var objectID = require('mongodb').ObjectID;
var assert = require('assert');
const url = 'mongodb://localhost:27017';
const dbName = 'ldex';
const tblOffers = 'offers';
router.post('/insert', function (req, res, next) {
var order = {
type: 'Sell',
..............
max: req.body.max,
protection: req.body.protection,
comment: req.body.comment,
date: new Date().toDateString()
};
mongo.connect(url, function (err, client) {
assert.equal(null, err);
const db = client.db(dbName);
db.collection(tblOffers).insertOne(order, function (err, res) {
assert.equal(null, err);
console.log('Offer placed');
client.close();
})
});
res.redirect('/exchange')
});
But here is another app, parser, that have the same mongo connection, it's adding some data in it from other web-site (when do not use mongo all ok, I recieve data well), here is the part of code:
...
let assert = require('assert');
let mongo = require('mongodb').MongoClient;
let router = express.Router();
const url = 'mongodb://localhost:27017/';
const dbName = 'ceramo';
const tblOffers = 'items';
...
/*
* Get items from specific group
*/
function getGroupItems(url, callback) {
request({uri: url}, function (error, response, body) {
let list = [];
let $ = cheerio.load(body);
$('#tovar').find('a.cat_item_disc_a').each(function(i, elem) {
list[i] = 'https://plitkazavr.ru' + $(this).attr('href');
});
callback(list);
});
}
/*
* Parse one item from specific link
*/
function getItem(url) {
request({uri: url}, function (error, response, body) {
let list = {};
let $ = cheerio.load(body);
$('#item_border').find('> #item_prop > ul').find('li.item_list').each(function(i, elem) {
list[$(this).find('.item_cell').first().text()] = $(this).find('div.item_cell.item_val').text();
});
list.price = (parseFloat($('#item_price').text()));
list.img = ('https://plitkazavr.ru' + $('#item_img').attr('src'));
list.meta = ($('#item_wrap').find('meta[itemprop="description"]').attr("content"));
mongo.connect(url, function (err, client) {
assert.equal(null, err);
const db = client.db(dbName);
db.collection(tblOffers).insertOne(order, function (err, res) {
assert.equal(null, err);
console.log('Offer placed');
client.close();
})
});
});
}
router.get('/parse', function (req, res, next) {
getGroupItems('https://plitkazavr.ru/Naxos/Clio', function (items) {
items.forEach(function(item, i, arr) {
setTimeout(function() {
getItem(item);
}, 1000);
});
});
res.end('ok');
});
And it's down on connection and gives the
`Error: Invalid schema, expected `mongodb` or `mongodb+srv
Don't understand where is a problem, help please...
Your MongoDB URL seems incomplete, it should include your dbName
before it's passed into the mongoose connection like this
const url = 'mongodb://localhost:27017/';
const fullUrl = url + dbName; // which should evaluate to this 'mongodb://localhost:27017/ceramo'
mongo.connect(fullUrl, function (err, client) {...}
这篇关于无效架构,应为“mongodb"或“mongodb+srv"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!