Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

node.js - MongoDB - Error: invalid schema, expected mongodb

I'm new in building application with MEAN Stack, I'm trying to build a real time chat app, here is my server side :

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

});

});

I am sure that i created a data base called chat with mongodb, also mongo is waiting for connection. But when i run the server with node server.js an error occurs :

Server running...!
C:UsersazusDesktopPsirtcodemaster
ode_modules mongodbliburl_parser.js:20
  throw new Error('invalid schema, expected mongodb');
  ^

Error: invalid schema, expected mongodb
at module.exports (C:UsersazusDesktopPsirtcode-master
ode_modulesmong
 odbliburl_parser.js:20:11)
at connect (C:UsersazusDesktopPsirtcode-master
ode_modulesmongodblib
 mongo_client.js:125:16)
at Function.MongoClient.connect (C:UsersazusDesktopPsirtcode-master
od
e_modulesmongodblibmongo_client.js:109:3)
at Object.<anonymous> (C:UsersazusDesktopPsirtcode-masterserver.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:UsersazusDesktopPsirtcode-master>

I had been blocked at this phase for weeks, could anyone help on this?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is because you are using the connection string in an improper format.

You are using localhost:27017/db/chat while it should be mongodb://localhost:27017/db/chat

The pattern for the connection string is mongodb://<HOSTNAME>:<PORT>/<DBNAME>

Article for reference: https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#mongoclient-connect


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...