0

I'm struggling to understand what's wrong with the following queries. I'm using the mongoose 4.1.10 and mongodb 3.0.6.

passport.use("local-signup", new passportLocal.Strategy({
    // by default, local strategy uses username and password, we will override with email
    usernameField : 'email',
    passwordField : 'password'
    //passReqToCallback : true // allows us to pass back the entire request to the callback
}, (username, password, done) => {
    co(function *() {
        try {
            const user = yield User.findOne({ "local.email": username });

            if (user) {
                return done(null, false);
            }
            else {
                const newUser = new User();

                newUser.local.email = username;
                yield newUser.save();
            }
        }
        catch(ex) {
            done(ex);
        }
    }).then((user) => {
        done(null, user);
    });
}));

More specifically User.findOne({ "local.email": username }); never resolves.

I have even tried to use the callback approach but again I don't get any results.

ppoliani
  • 4,792
  • 3
  • 34
  • 62
  • is `done` defined somewhere else? you should return value from generator or throw an error – Tim Marinin Oct 12 '15 at 20:04
  • Look at the updated question. Actually, you're not right; I'm yielding values from generator. You don't have to explicitly have a return statement. In fact, you should omit returning values. It's better using `yield` for that – ppoliani Oct 12 '15 at 20:16
  • I'm not sure you want to be calling `done()` from your `then()`. It seems the code as is, will sometimes call `done()` twice. Once from within the generator and then again from the then() – James Moore Oct 12 '15 at 20:46
  • What's the function `co` you're calling from the start of your anonymous function? – piemonkey Oct 12 '15 at 21:12
  • I see, should have actually looked up the 'co' library before commenting... – piemonkey Oct 12 '15 at 21:17

1 Answers1

0

User.findOne({ "local.email": username }) returns a Query, you need to call .exec() to return a Promise, more info in the docs.

piemonkey
  • 741
  • 3
  • 6