TypeError:无法读取属性' 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;
邮差要求
我正在尝试使用multer和multer-gridFs-Storage上传图像,但是正面临着这个问题。两天以来我一直被困在这一点上。不知道问题的根本原因。似乎在createWriteStream点我在控制台中收到此错误。有什么建议?
答案 0 :(得分:0)
似乎Gridfs与大于5.0.0的新Mongoose版本不兼容。因此,尝试使用的版本要少一些,然后一切正常。