【发布时间】:2019-11-11 11:48:02
【问题描述】:
我想从 MongoDB GridFs 读取文件并将其附加到从节点邮件程序发送的邮件中。所以我需要在服务器端读取这个文件,这意味着我无权访问 http 请求或响应对象,我无法通过管道将读取流传输到互联网上多个地方建议的响应。
我读取文件并作为缓冲区发送的代码如下 -
let _fetchFileById = async (fileId, options) => {
let db = options.db
, gfs = Grid(db, mongo)
, filename = fileId
, file_buf = new Buffer('buffer');
return new Promise((resolve,reject) => {
gfs.collection('jobdescription')
let readstream = gfs.createReadStream({
filename: filename
, root: 'jobdescription'
})
readstream.on('data', function (chunk) {
console.log("writing!!!");
// file_buf.push(chunk)
if (!file_buf)
file_buf = chunk;
else file_buf = Buffer.concat([file_buf, chunk]);
});
readstream.on('end', function () {
// var buffer = Buffer.concat(file_buf);
db.close();
console.log(`returning`)
// console.log(file_buf)
resolve(file_buf)
})
})
}
当我在节点邮件程序中使用 file_buf 作为附件的输入时,我得到以下错误 -
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object。任何帮助或指示将不胜感激。
【问题讨论】:
标签: node.js mongodb nodemailer gridfs