Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtual.get is not a function? #7

Closed
aharris02 opened this issue Jul 15, 2017 · 1 comment
Closed

virtual.get is not a function? #7

aharris02 opened this issue Jul 15, 2017 · 1 comment

Comments

@aharris02
Copy link

aharris02 commented Jul 15, 2017

Hi -- I'm having some trouble getting this to work. My project structure:

app.js
models/ (mongoose models)
--user.js
routes/ (expressjs routes)
--user.js

models/user.js:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var Pictoose = require('pictoose');

// Must label separate db connections to prevent connection conflicts
var conn3 = mongoose.createConnection('localhost:27017/user');

var userSchema = new Schema({
    email: {type: String, required: true},
    {...edited for brevity...}
    userImage1: {data: Buffer, contentType: String, required: false},
    userImage2: {data: Buffer, contentType: String, required: false},
    userImage3: {type: Buffer, required: false},
});

userSchema.methods.encryptPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
};

userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
};

userSchema.plugin(Pictoose.Plugin, ['userImage1','userImage2','userImage3']);

module.exports = conn3.model('User', userSchema);

routes/user.js

var express = require('express');
var router = express.Router();
var csrf = require('csurf');
{...unnecessary libraries edited out...}
var User = require('../models/user');
var multer = require('multer');
var Pictoose = require('pictoose');

// Config for Pictoose -- storing images in MongoDB via Mongoose
Pictoose.Config('RESOURCE_STORAGE_ROOT', './uploads/');
Pictoose.Config('RESOURCE_STORAGE_URL', 'http://127.0.0.1:3000/user/');
Pictoose.Config('RESOURCE_MAIN_URL', 'http://127.0.0.1:3000/resources/');
    
router.use('/user',express.static('./uploads'));
router.get('/resources/:resid', Pictoose.RouteController);

// Registration - Upload ID
router.get('/upload-id', ensureLoggedIn('/user/signin'), function(req, res) {

    res.render('user/create/upload-id', { csrfToken: req.csrfToken() });
});

router.post('/upload-id', function(req, res) {
        var token = req.user.verifyAccountToken;

        if (!req.user) {
            req.flash('error', 'The link you followed is no longer valid.');
            return res.redirect('/user/signin');
        }
        // store images
        try {
            user.userImage1.data = fs.readFileSync(req.files.userImage1.path);
            user.userImage1.contentType = 'image/png';
            user.userImage2.data = fs.readFileSync(req.files.userImage2.path);
            user.userImage3.contentType = 'image/png';
            user.userImage3.data = fs.readFileSync(req.files.userImage3.path);
            user.userImage.contentType = 'image/png';
            user.save(function (err, a) {
                if (err) throw err;
                res.redirect('/user/profile');
            });
        } catch (err) {
            req.flash('error', err);
            res.redirect('/user/signup');
        }
    res.redirect('/user/profile');
});

The error I'm seeing:

D:\Desktop\website\node_modules\pictoose\pictoose.js:298
                schema.virtual(fields[i]).get(PictureGetter(fields[i]));
                                          ^

TypeError: schema.virtual(...).get is not a function
    at Plugin (D:\Desktop\website\node_modules\pictoose\pictoose.js:298:29)
    at Schema.plugin (D:\Desktop\website\node_modules\mongoose\lib\schema.js:1162:3)

The error seems to be caused by this line:

userSchema.plugin(Pictoose.Plugin, ['userImage1','userImage2','userImage3']);

I've tried a few variants based on the documentation, but none seem to work. Any suggestions on what I'm doing wrong? Thanks!

@aharris02
Copy link
Author

Closing issue as I found a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant