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

Getting the _doc undefined error. #6

Open
shellybranch2018 opened this issue Mar 5, 2019 · 1 comment
Open

Getting the _doc undefined error. #6

shellybranch2018 opened this issue Mar 5, 2019 · 1 comment

Comments

@shellybranch2018
Copy link

I'm following along in the course, building the app and tried running the first query in #7, Dynamic Relations.

query {
events {
creator {
email
}
}
}
Result from terminal
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property '_doc' of undefined.

@shellybranch2018
Copy link
Author

Here is my code:
rootValue: {

    events: () => {
        Event.find().populate('creator')
        .then(events => {
              return events.map(event =>{
                return { ...event._doc,
                     _id: event.id,
                    creator: {...event._doc.creator._doc,
                    _id: event._id
                    
                    }
                };
                
            });
        }).catch(err => {
            throw err;
        });
    },
    createEvent: (args) => {
    const event = new Event({
        title: args.eventInput.title,
        description: args.eventInput.description,
        price: +args.eventInput.price,
        date: new Date(args.eventInput.date),
        creator: '5c7ec597d0578c1420b3894c'
        
    });
    let createdEvent;
     return event
     .save()
     .then(result => {
         createdEvent = {...result._doc, _id: result._doc._id.toString()};
       return User.findById('5c7ec597d0578c1420b3894c')
        
     })
     .then(user => {
        if(!user) {
            throw new Error('User not found.');
        }
        user.createdEvents.push(event);
        return user.save();
     })
    //  .then(result =>{
    //     return createdEvent;
    //  })
     .catch(err => {
         console.log(err)
         throw err;
     });
    
    },
    createUser: (args) => {
        return User.findOne({
            email: args.userInput.email
        }).then(user => {
            if(user) {
                throw new Error('User exist already.')
            }
            return bcrypt.hash(args.userInput.password, 12)
        })
        .then(hashedPassword => {
            const user = new User({
                email: args.userInput.email,
                password: hashedPassword
            });
           
            return user.save();
        })
        .then(result => {
           
            return {...result._doc, password: null, _id: result.id };
        })
        
        .catch(err => {
            throw err;
        })
        
    }
},
graphiql: true

})

);

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