Reputation: 660
My code below is attempting to:
User
modelSubscriber
model with the same email address as the newly created usersubscribedAccount
property to the Subscriber
instance found by the findOne
query on the user.email
Code:
// Check that I have a subscriber with email '[email protected]'
Subscriber.findOne({email:'[email protected]'})
.then(d => console.log(`\nResult of check for a subscriber with email [email protected]:\n ${d}`));
User.create({name: {first: 'test first', last: 'test last'}, email: '[email protected]', password: 'pass123'})
.then(u => {
user = u;
// Check that user.email contains '[email protected]'
console.log(`\nCreated user's email address: ${user.email}\n`);
Subscriber.findOne({email: user.email});
})
.then(s => {
console.log(`\nAnything found by findOne and passed to this .then()?: ${s}`);
user.subscribedAccount = s;
user.save();
})
.catch(e => console.log(e.message));
Console results:
Server running at http://localhost:3000 Successfully connected with Mongoose!
Result of check for a subscriber with email [email protected]:
{ groups: [], _id: 5aa422736518f30fbc0f77e2, name: 'test name', email: '[email protected]', zipCode: 11111, __v: 0 }Created user's email address: [email protected]
Anything found by findOne and passed to this .then()?: undefined
Why is Subscriber.findOne
returning undefined
? Is that what is actually happening or is it something else I'm missing?
Here are my model definitions for User
and Subscriber
. Let me know if you need to see anything else from the application to tell what is going on.
User
:
const mongoose = require('mongoose');
const {Schema} = require('mongoose');
var userSchema = new Schema( {
name: {
first: {
type: String,
trim: true
},
last: {
type: String,
trim: true
}
},
email: {
type: String,
required: true,
lowercase: true,
unique: true
},
zipCode: {
type: Number,
min: [ 10000, 'Zip code too short' ],
max: 99999
},
password: {
type: String,
required: true
},
courses: [ {
type: Schema.Types.ObjectId,
ref: 'Course'
} ],
subscribedAccount: {
type: Schema.Types.ObjectId,
ref: 'Subscriber'
}
}, {
timestamps: true
} );
userSchema.virtual('fullName').get(function() {
return `${this.name.first} ${this.name.last}`;
});
module.exports = mongoose.model('User', userSchema);
Subscriber
:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let subscriberSchema = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
lowercase: true,
unique: true
},
zipCode: {
type: Number,
min: [10000, 'Zip Code too short'],
max: 99999
},
groups: [{type: Schema.Types.ObjectId, ref: 'Group'}]
});
subscriberSchema.methods.getInfo = function() {
return `Name: ${this.name} Email: ${this.email} Zip Code: ${this.zipCode}`;
}
subscriberSchema.methods.findLocalSubscribers = function() {
return this.model('Subscriber')
.find({zipCode: this.zipCode})
.exec();
}
//model.exports = mongoose.model('Subcriber', subscriberSchema);
var Subscriber = exports.Subscriber = mongoose.model('Subscriber', subscriberSchema);
Upvotes: 2
Views: 46
Reputation: 890
You should have done like this
// Check that I have a subscriber with email '[email protected]'
Subscriber.findOne({email:'[email protected]'})
.then(d => console.log(`\nResult of check for a subscriber with email [email protected]:\n ${d}`));
User.create({name: {first: 'test first', last: 'test last'}, email: '[email protected]', password: 'pass123'})
.then(u => {
user = u;
// Check that user.email contains '[email protected]'
console.log(`\nCreated user's email address: ${user.email}\n`);
Subscriber.findOne({email: user.email});
console.log(`\nAnything found by findOne and passed to this .then()?: ${s}`);
user.subscribedAccount = s;
user.save()
.then(s => {
//user has been updated
})
.catch(err => {
res.status(err).json(err);
})
})
})
. catch(e => console.log(e.message));
Upvotes: 1