0

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 点我在控制台中收到此错误。有什么建议么?

4

1 回答 1

0

似乎 Gridfs 与大于 5.0.0 的新 Mongoose 版本不兼容。所以尝试使用少于那个版本的版本,那么一切都会正常工作。

于 2018-10-06T13:14:52.927 回答