【问题标题】:Initializing Gridfs returns TypeError: string is not a function初始化 Gridfs 返回 TypeError: string is not a function
【发布时间】:2015-07-20 19:27:27
【问题描述】:

我正在尝试将上传的文件(例如图像)写入 mongodb 数据库。但是,当我尝试初始化 gridfs-stream 时,我得到一个类型错误。这是我的代码:

var mongo = require('mongodb');

router.post('/createpost', cpUpload, function(req,res) {
  var db = req.db;

  var gfs = Grid(db,mongo);

  var file = req.file;

  var fileId = new ObjectId();

  var writestream = gfs.createWriteStream({
    filename: file.originalname,
    mode: 'w',
    content_type: file.mimetype,
    metadata: req.body.postText,
  });
  fs.createReadStream(file.path).pipe(writestream);

  writestream.on('close', function(file) {
    res.send("Succcess!");
    fs.unlink(file.path,function(err) {
      if (err) {
        console.log("Error: ", err);
        console.log("DELETED: ", file.path);
      }
  });
 });


});

我猜这与我将db 称为req.db 有关,但我使用 app.js 中的此代码使路由器文件可以访问数据库

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/test-api');

app.use(function(req,res,next) {
  req.db = db;
  req.mongo = mongo;
  next();
});

【问题讨论】:

  • 我在下面的回答修复了string is not a function 错误,但我仍然没有让这段代码工作。我发布了另一个问题here

标签: node.js mongodb express gridfs monk


【解决方案1】:

var Grid = require('gridfs-stream'); 的语法不正确,很容易修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2019-06-06
    • 2013-10-30
    • 2021-01-21
    相关资源
    最近更新 更多