gpt4 book ai didi

node.js - 错误 ReferenceError : ObjectID is not defined

转载 作者:可可西里 更新时间:2023-11-01 09:06:45 29 4
gpt4 key购买 nike

我想用 GridJS 上传图片。我正在使用 https://www.npmjs.org/package/gridfs-stream为了这。它给我 error ReferenceError: ObjectID is not defined

我是初学者,所以不知道发生了什么。

如何解决这个错误?

var Grid = require('gridfs-stream');
var conn = mongoose.createConnection('localhost', 'Test', 27017);
var gfs = Grid(conn.db, mongoose);

var fileId = new ObjectID(uniqId);
var fileType = req.header('X-File-Type');
var fileName = req.header('X-File-Name');
var uniqId = req.param('uniqId', '112211112111');

console.log('upload start');
// streaming to gridfs
var writestream = gfs.createWriteStream(fileId, [{ "content_type": fileType, "metadata": { "fileName": fileName, "uploaded_at": (new Date()).toString() } }]);
fs.createReadStream('/some/path').pipe(writestream);

// streaming from gridfs
var readstream = gfs.createReadStream(fileId, [{ "content_type": fileType, "metadata": { "fileName": fileName, "uploaded_at": (new Date()).toString() } }]);

//error handling, e.g. file does not exist
readstream.on('error', function (err) {
console.log('An error occurred!', err);
throw err;
});

readstream.pipe(response);
console.log('upload done');

最佳答案

ObjectID 不是全局变量,它由 mongoose 定义(参见 documentation ):

var fileId = mongoose.Types.ObjectId();

关于node.js - 错误 ReferenceError : ObjectID is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143090/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com