无效的架构,预期的mongodb

Ahm*_*202 37 mongodb node.js mean-stack

我是使用MEAN Stack构建应用程序的新手,我正在尝试构建一个实时聊天应用程序,这是我的服务器端:

console.log("Server running...!");

var mongo=require('mongodb').MongoClient;
var client=require('socket.io').listen(8080).sockets;

mongo.connect('localhost:27017/db/chat',function(err,db){
if(err)  throw err;

client.on('connection',function(socket){
console.log('someone has connected !');

//waiting for input
socket.on('input',function(data){
console.log(data);
});

});

});
Run Code Online (Sandbox Code Playgroud)

我确信我创建了一个名为chat with mongodb的数据库,mongo也在等待连接.但是当我使用节点server.js运行服务器时会发生错误:

Server running...!
C:\Users\azus\Desktop\Psirt\codemaster\node_modules\ mongodb\lib\url_parser.js:20
  throw new Error('invalid schema, expected mongodb');
  ^

Error: invalid schema, expected mongodb
at module.exports (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mong
 odb\lib\url_parser.js:20:11)
at connect (C:\Users\azus\Desktop\Psirt\code-master\node_modules\mongodb\lib
 \mongo_client.js:125:16)
at Function.MongoClient.connect (C:\Users\azus\Desktop\Psirt\code-master\nod
e_modules\mongodb\lib\mongo_client.js:109:3)
at Object.<anonymous> (C:\Users\azus\Desktop\Psirt\code-master\server.js:6:8
)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:139:18)

C:\Users\azus\Desktop\Psirt\code-master>
Run Code Online (Sandbox Code Playgroud)

我已经在这个阶段被阻止了几个星期,请帮忙

Jon*_*ler 111

这是因为您以不正确的格式使用连接字符串.

你正在使用localhost:27017/db/chat它应该是mongodb://localhost:27017/db/chat

连接字符串的模式是 mongodb://<HOSTNAME>:<PORT>/<DBNAME>

文章供参考:https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#mongoclient-connect


Mik*_*eel 7

我也遇到过这个问题,因为我的协议错了:

mongo://localhost:27017/test
Run Code Online (Sandbox Code Playgroud)

协议错误也可能导致此错误.它应该是这样的:

mongodb://localhost:27017/test
Run Code Online (Sandbox Code Playgroud)


pro*_*per 5

Sometimes, error might be with the quotes around environment variables. Remove them once and try. Might help.

Error might be with :

 set DATABASE_URI='mongodb://localhost:1000/my_app' && node index.js
Run Code Online (Sandbox Code Playgroud)

Correct command will be:

  set DATABASE_URI=mongodb://localhost:1000/my_app && node index.js
Run Code Online (Sandbox Code Playgroud)

  • 刚刚对“docker-compose”环境变量遇到了同样的问题。我不需要将其用任何引号(单引号或双引号)括起来。这对我有用。`MONGODB_URI=mongodb://mongo:27017/` (2认同)