【发布时间】:2020-07-29 19:47:05
【问题描述】:
我想使用 mongoose-gridfs 上传图片。我遇到了以下问题(我在下面的照片中显示的问题)。它是使用 mongoose-gridfs 的,我想如果有人使用过这个库,并且按照我在下面构建它的方式,它将帮助我解决我的情况或更新我,如果有更新的方法来做到这一点遵循相同的结构。提前致谢。
This image show the error when i try to execute my code
这是我的模型附件
'use strict'
const mongoose = require('mongoose');
const gridfs = require('mongoose-gridfs')({ // TypeError: require(...) is not a function
collection: 'attachments',
model: 'Attachment'
});
const AttachmentSchema = gridfs.schema;
module.export = mongoose.model('Attachment', AttachmentSchema);
这是我的模型用户,我将附件作为参考传递给照片属性
'use strict'
require('mongoose-type-email');
const bcrypt = require('bcrypt-nodejs');
const crypto = require('crypto');
const uniqueValidator = require('mongoose-unique-validator');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
first_name: {type: String, required: true},
email: {type: mongoose.SchemaTypes.Email, required: true, unique: true},
password: {type: String, required: true},
active: {type: Boolean, default: false},
created: {type: Date, default: Date.now()},
photo: {type: Schema.ObjectId, ref: "Attachment"},
valid_token: {type: String},
});
现在是我用来上传图片的 fileService.js 服务。
'use strict'
const Attachment = require('../models/Attachment');
const im = require('imagemagick');
const fs = require('fs');
const events = require('events');
const gridfs = require('mongoose-gridfs')({ //TypeError: require(...) is not a function
collection: 'attachments',
model: 'Attachment'
});
exports.uploadFile = function (file, cb) {
const eventEmitter = new events.EventEmitter();
const lista = Object.values(file);
const properties = Object.getOwnPropertyNames(file);
const result = [];
const listLength = lista.length;
eventEmitter.once('err', err => {
cb(err);
});
eventEmitter.once('upload', lista => {
cb(null, lista);
});
eventEmitter.on('uploadFinish', (obj, property) => {
obj.contentType.startsWith("image") ? result.push({
type: 'img',
'property': property,
'value': obj
}) : result.push({type: 'video', 'property': property, 'value': obj})
if (listLength == result.length)
eventEmitter.emit("upload", result);
});
lista.length != 0 ? fileWrite(lista, properties, eventEmitter) : eventEmitter.emit("upload");
};
function fileWrite(lista, properties, eventEmitter) {
const files = Array();
const Attachment = gridfs.model;
lista.forEach((element, index) => {
Attachment.write({
filename: element.name,
contentType: element.type
},
fs.createReadStream(element.path),
(err, createdFile) => {
err ? eventEmitter.emit('err', err) : eventEmitter.emit('uploadFinish', createdFile,
properties[index]);
});
});
}
这是我的 userService.js,我在其中调用 fileService.js 服务的 uploadFile 函数
'use strict'
const User = require('../models/User');
const fileService = require('../services/fileService');
exports.save = function (file, data, res) {
fileService.uploadFile(file, (err, files) => {
if (!err) {
if (files) {
files.forEach((e) => {
if (e.property == "photo") {
data.photo = e.value;
}
});
}
data['created'] = new Date();
const user = new User(data);
user.save((err, user) => {
if (!err) {
user.update(data, (error) => {
if (!error) {
res.json({success: true, message: "Inserted user", data: user})
} else {
res.send({
success: false,
message: 'User cant be updated'
});
}
});
} else
res.status(500).send({success: false, message: 'Error trying to save the user'});
});
} else {
res.status(500).send({success: false, message: 'Error trying to upload files'});
}
});
};
【问题讨论】:
-
我想建议您不要将图像存储在 mongo 使用文件系统中,以提高速度和充分利用
标签: node.js mongodb mongoose gridfs