- 类型错误:无法读取未定义**的属性"readPreference"
E:\CapstoneProject\Express_ws\FoodCourtServer\node_modules\mongodb\lib\gridfs\grid_store.js:134
this.options.readPreference || db.options.readPreference || ReadPreference.primary;
^
TypeError: Cannot read property 'readPreference' of undefined
at new GridStore (E:\CapstoneProject\Express_ws\FoodCourtServer\node_modules\mongodb\lib\gridfs\grid_store.js:134:47)
at new GridWriteStream (E:\CapstoneProject\Express_ws\FoodCourtServer\node_modules\gridfs-stream\lib\writestream.js:64:16)
at Grid.createWriteStream (E:\CapstoneProject\Express_ws\FoodCourtServer\node_modules\gridfs-stream\lib\index.js:42:10)
at E:\CapstoneProject\Express_ws\FoodCourtServer\routes\register.js:108:31
at Immediate._onImmediate (E:\CapstoneProject\Express_ws\FoodCourtServer\node_modules\multer\lib\make-middleware.js:53:37)
at runCallback (timers.js:793:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5
- 服务器代码(register.js)**
var mongoose = require('mongoose');
var multer = require('multer');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var Readable = require('stream').Readable;
var GridFsStorage = require('multer-gridfs-storage');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27050/FoodCourtDatabase');
var conn = mongoose.connection;
conn.once('open', function () {
console.log('Connected to DB.');
});
conn.on('error', function (err) {
console.log('Connection error', err);
});
var RegisteredUserApi = require('../data/FoodCourtApi');
var express = require('express');
var router = express.Router();
// var busboy = require('connect-busboy');
// var app = express();
// app.use(busboy());
var gfs = Grid("FoodCourtDatabase");
var storage = GridFsStorage({
url: 'mongodb://localhost:27050/FoodCourtDatabase',
gfs: gfs,
file: function (req, file) {
console.log('file : '+JSON.stringify(file,null,4));
return { filename: req.body.name };
}
});
// multer settings for single upload
var upload = multer({
storage: storage
}).single('profilepict');
//This route is only for testing
router.get('/', function (req, res) {
res.json({ Hi: 'karthik' });
});
router.post('/loginvalidation', function (req, res) {
var loggedInUser = {};
loggedInUser.partnerId = req.body.partnerId;
loggedInUser.password = req.body.password;
RegisteredUserApi.validateLoggedInUser(loggedInUser, function (err, data) {
// res.end();
if (err) {
console.log(err);
res.status(500).json({ error: "login validation failed", err: err });
} else {
res.json(data);
}
});
});
router.get('/fetchprofiledetails/:id', function (req, res) {
RegisteredUserApi.getProfileDetails(req.params.id, function (err, data) {
if (err) {
res.send(err);
} else {
res.json(data);
}
});
});
router.post('/uploadprofile', function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.status(400).json({ message: 'Upload Request Validation Failed' });
} else if (!req.body.name) {
return res.status(400).json({ message: 'No Picture name in request body' });
}
var picName = req.body.name;
// convert buffer to readable stream
var readableTrackStream = new Readable();
readableTrackStream.push(req.file.buffer);
readableTrackStream.push(null);
var writestream = gfs.createWriteStream({ filename: picName });
var id = writestream.id;
readableTrackStream.pipe(writestream);
writestream.on('error', function (err) {
return res.status(500).json({ message: 'Error uploading file ' });
}).on('finish', function () {
return res.status(201).json({ message: 'File uploaded successfully, stored under Mongo ObjectID ' + id });
});
});
});
module.exports = router;
- Postman 请求**
Request payload in postman
我尝试使用multer和multer-gridFs-Storage上传图像,但遇到了这个问题。我在这一点上卡住了两天。不知道问题的根本原因。它似乎在createWriteStream点我在控制台中得到这个错误。任何建议?
2条答案
按热度按时间rjee0c151#
Gridfs似乎与大于5.0.0的新Mongoose版本不兼容。因此,尝试使用小于5.0.0的版本,然后一切都会正常工作。
5n0oy7gb2#
请使用此选项:
代替