471639 (6) [Avatar] Offline
#1
I've tried the code in Listing 18.6 in REPL and in main.js, but I can't seem to get anything to return from the Subscriber.findOne call.

I've checked repeatedly, and I do, in fact, have a 'Jon Wexler' subscriber with email address 'jon@jonwexler.com'. It just doesn't get returned and subsequently, cannot be associated to the User instance created.

Is there an issue with the code in the listing? Is there something I'm missing?

The code I'm running (slight modification of Listing 18.6, just to check that I have the subscriber instance and to see if anything is returned from findOne):
// Check for a subscriber with email 'jon@jonwexler.com'
Subscriber.findOne({email:'jon@jonwexler.com'}).then(d => console.log(d));

// Minimally-modified Listing 18.6 code
User.create({name: {first: 'Jon', last: 'Wexler '}, email: 'jon@jonwexler.com', password: 'pass123'})
.then(u => {
  user = u;
  // Check what email is being used in the findOne call
  console.log(`\nCreated user's email address: ${user.email}\n`); 
  Subscriber.findOne({email: user.email});
})
.then(s => {
  // Check what is passed into .then
  console.log(s);
  user.subscribedAccount = s;
  user.save();
})
.catch(e => console.log(e.message));


My console output:
Server running at http://localhost:3000
Successfully connected with Mongoose!
{ groups: null,
_id: 5a8f38056c3c5a2a60a97715,
name: 'Jon',
email: 'jon@jonwexler.com',
zipCode: 10009,
__v: 0 }

Created user's email address: jon@jonwexler.com

undefined
471639 (6) [Avatar] Offline
#2
Ok, it looks like Listing 18.6 just needs a `return` on the last line of the first `.then()`:

return Subscriber.findOne({email: user.email});
TheWexler (56) [Avatar] Offline
#3
My apologies for the delayed response. You're right, the call to find a Subscriber by that email address needs to be returned. Good catch! That returned query is actually a promise, which feeds into the next .then block. Thanks for pointing this out.