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

Problem with babel #26

Closed
iraniamir opened this issue Nov 19, 2017 · 5 comments
Closed

Problem with babel #26

iraniamir opened this issue Nov 19, 2017 · 5 comments
Labels

Comments

@iraniamir
Copy link

if we use babel we got this error : (i convert this class to es5 and works, i think you missed something)

/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                  ^

TypeError: Class constructor MongoModels cannot be invoked without 'new'
    at new Session (/home/amir/Desktop/NodeJs/cannal/server/models/session.js:23:103)
    at Function.resultFactory (/home/amir/Desktop/NodeJs/cannal/node_modules/mongo-models/index.js:119:26)
    at handleCallback (/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/lib/utils.js:120:56)
    at /home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/lib/collection.js:1416:5
    at handleCallback (/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/lib/utils.js:120:56)
    at /home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/lib/cursor.js:683:5
    at handleCallback (/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:171:5)
    at nextFunction (/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:689:5)
    at /home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:600:7
    at queryCallback (/home/amir/Desktop/NodeJs/cannal/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:232:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Cannal@7.0.2 babel-node: `babel-node --presets=es2015 --inspect --debug "./server.js"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the Cannal@7.0.2 babel-node script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/amir/.npm/_logs/2017-11-19T15_24_35_238Z-debug.log
@jedireza
Copy link
Owner

You may want to make an exception and not run your models through a transpiler like babel.

Though I am curious how are you using the constructor without the new keyword. Can you share example code that produces this error?

@iraniamir
Copy link
Author

i exactly use aqua source, this is my code https://github.com/jedireza/aqua/blob/master/server/models/session.js

@jedireza
Copy link
Owner

That's the model. However it sounds like how that model get's instantiated is the problem (not using a new keyword.

Sharing a reproducible code snippet that I can run to get the same error would be most helpful.

@iraniamir
Copy link
Author

iraniamir commented Nov 23, 2017

'use strict';
const Async = require('async');
const Bcrypt = require('bcrypt');
const Joi = require('joi');
const MongoModels = require('mongo-models');
const Uuid = require('uuid');

class Session extends MongoModels {

    static generateKeyHash(callback) {

        const key = Uuid.v4();

        Async.auto({
            salt: function (done) {

                Bcrypt.genSalt(10, done);
            },
            hash: ['salt', function (results, done) {

                Bcrypt.hash(key, results.salt, done);
            }]
        }, (err, results) => {

            if (err) {
                return callback(err);
            }

            callback(null, {
                key,
                hash: results.hash
            });
        });
    }

    static create(userId, callback) {

        const self = this;

        Async.auto({
            keyHash: this.generateKeyHash.bind(this),
            newSession: ['keyHash', function (results, done) {

                const document = {
                    userId,
                    key: results.keyHash.hash,
                    time: new Date()
                };

                self.insertOne(document, done);
            }],
            clean: ['newSession', function (results, done) {

                const query = {
                    userId,
                    key: { $ne: results.keyHash.hash }
                };

                self.deleteOne(query, done);
            }]
        }, (err, results) => {

            if (err) {
                return callback(err);
            }

            results.newSession[0].key = results.keyHash.key;

            callback(null, results.newSession[0]);
        });
    }

    static findByCredentials(id, key, callback) {

        const self = this;

        Async.auto({
            session: function (done) {

                self.findById(id, done);
            },
            keyMatch: ['session', function (results, done) {

                if (!results.session) {
                    return done(null, false);
                }

                const source = results.session.key;
                Bcrypt.compare(key, source, done);
            }]
        }, (err, results) => {

            if (err) {
                return callback(err);
            }

            if (results.keyMatch) {
                return callback(null, results.session);
            }

            callback();
        });
    }
}

Session.collection = 'sessions';

Session.schema = Joi.object({
    _id: Joi.object(),
    userId: Joi.string(),
    key: Joi.string(),
    time: Joi.date()
});

Session.constructWithSchema = true;

Session.indexes = [
    { key: { userId: 1 } }
];

module.exports = Session;

@jedireza
Copy link
Owner

This isn't a reproducible code example, just the code that you linked to. At this point I'm unable to help since I don't have enough information to work with. I'd also venture to say that you shouldn't need to transpile your model code since it's not intended for the browser.

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

No branches or pull requests

2 participants