0
使用此代碼在mlab上託管的MongoDB數據庫上創建集合。但不知怎的,它似乎並沒有工作。在這段代碼中是否有我缺少的東西? .save()函數似乎根本不會觸發。它可能是由於我的模式?無法在MongoDB數據庫中創建集合
var mongoose= require('mongoose');
var Schema= mongoose.Schema;
app.use(express.static(__dirname+'/views'));
app.use(express.static(path.join(__dirname, 'public')));
//connect to mongo db database
mongoose.connect('mongodb://blaa:[email protected]:27132/vendor');
//vendor schema
var vendorSchema= new Schema({
name:String,
image: { data: Buffer, contentType: String },
vendortype:String,
location: {
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
},
contactinfo:String,
description:String
});
//creating a model for mongoDB database
var Vendor= mongoose.model('Vendor',vendorSchema);
//just putting a sample record data
var imgPath = 'public/images/background.jpg';
var one = Vendor({
name: 'Justin Motor Works',
vendortype: 'Automobile',
contactinfo:'6764563839',
location: {
type:[23.600800037384033,46.76758746952729]
},
image: {
data: fs.readFileSync(imgPath),
contentType: 'image/jpg'
},
description: 'Motor workshop'
}).
save(function(err){
if(err)
throw err;
else {
console.log('create record failed');
}
});
您如何測試?你是否啓動服務器以便連接? –
好吧,我只是啓動一個服務器並呈現一個HTML文件。我正在尋找任何新創建的集合的數據庫。 –
做這一行mongoose.connect('mongodb:// blaa:[email protected]:27132/vendor');數據庫沒有連接時不應該拋出錯誤。我用mongo終端嘗試了URL,它說它連接正確 –