如何连接数据库:mongoose?

时间:2017-05-01 03:09:06

标签: node.js mongodb mongoose gridfs gridfs-stream

以下是我的代码:文件夹和文件名为:gridFs/storeAudio.js。代码位于storeAudio.js

var mongoose = require('mongoose');
var fs = require('fs');
var Grid = require('gridfs-stream');
Grid.mongo=mongoose.mongo;


//establish mongoDB connection

var conn = mongoose.createConnection('mongodb://localhost:27017/aHolyBoly');
conn.once('open',function(){
  //  var gfs = Grid(conn.db);
 // var db = new mongo.Db('aHolyBoly', new mongo.Server("127.0.0.1", 27017));
//var gfs = Grid(db, mongo);

var writeStream = gfs.createWriteStream({
    filename:'song1.mp3'
});
fs.createReadStream('../list/hero.mp3').pipe(writeStream);
writeStream.on('close',function(file){
    console.log(file.filename +'Written to db');
});
});

现在在gridFs文件中的cmd中我正在运行命令node storeAudio.js来创建带有集合的数据库。但是,附带的屏幕截图中显示以下错误: enter image description here

注意:我之前运行了以下命令:npm install gridfs-streamnpm install mongoose

如果需要更多输入,请告诉我。

0 个答案:

没有答案