简体   繁体   中英

mongoose error node_modules error mongodb

I'm just following along with a screen cast verbatim and I'm getting an error in a node modules file, could you help?

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

mongoose.connect('mongodb//localhost/test');

var productModel = new Schema({
    productId: ObjectId,
    name: String,
    description: String,
    price: Number
});

var Product = mongoose.model('Product', productModel);

var sandal = new Product({
    name: 'sandal',
    description: 'something for your feet',
    price: 12
});

sandal.save(function(err) {
    if (err) console.log(err);

    console.log('sandal created!');
});

Product.find({
    name: 'sandal'
}).exec(function(err, product) {
    if (err) console.log(err);

    console.log(product);
});

and heres error

ryan@\Ryan:~/Desktop/node/datastores$ node Mongoose.js

/home/ryan/Desktop/node/node_modules/mongoose/node_modules/mongodb/lib/db.js:1550
   if(databaseName.indexOf(invalidChars[i]) != -1) throw MongoError.create({me
                                                                    ^
MongoError: database names cannot contain the character '/'
    at Function.MongoError.create (/home/ryan/Desktop/node/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
    at validateDatabaseName (/home/ryan/Desktop/node/node_modules/mongoose/node_modules/mongodb/lib/db.js:1550:70)
    at new Db (/home/ryan/Desktop/node/node_modules/mongoose/node_modules/mongodb/lib/db.js:133:3)
    at NativeConnection.doOpen (/home/ryan/Desktop/node/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:48:13)
    at NativeConnection.Connection._open (/home/ryan/Desktop/node/node_modules/mongoose/lib/connection.js:436:15)
    at NativeConnection.Connection.open (/home/ryan/Desktop/node/node_modules/mongoose/lib/connection.js:268:8)
    at Mongoose.connect (/home/ryan/Desktop/node/node_modules/mongoose/lib/index.js:227:15)
    at Object.<anonymous> (/home/ryan/Desktop/node/datastores/Mongoose.js:7:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

You're missing a semicolon in your URI

It should be:

mongoose.connect('mongodb://localhost/test');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM