无法使用mongoose-gridfs从MongoDB检索图像

时间:2018-08-29 12:34:23

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

我已经将图像与相应的集合fs.files和fs.chunks一起存储在mongoDB数据库CompanyDetails中。当我尝试使用mongoose-gridfs API和方法检索图像asus1.jpg时,返回的值未定义。请对此提供帮助,我缺少什么吗?我在控制台中显示内容,并且返回未定义。

mongoose-gridfs的文档链接-> https://www.npmjs.com/package/mongoose-gridfs

Fetch.js //我正在执行的用于连接和检索

var fs = require('fs');
var mongoose = require('mongoose');
var gridfs = require('mongoose-gridfs');
mongoose.connect('mongodb://localhost:27017/CompanyDetails',{useNewUrlParser:true})
.then(()=> console.log("mongoose is up"))
mongoose.Promise = Promise
mongoose.connection.on('connected', function () {  
  gridfs = require('mongoose-gridfs')({
  collection:'fs',
  model:'File',
  mongooseConnection: mongoose.connection
});

Attachment = gridfs.model;

var attachment = new Attachment({
  filename:'asus1.jpg',
  contentType:'image/jpeg'
});

attachment.read(function(error, content){
  console.log("this is the"+content);
})

}); 

0 个答案:

没有答案