menu

JS Everywhere

A community for the JavaScript Everywhere book by Adam Scott, published by O'Reilly.

Channels
Team

GraphQL Playground signIn error

May 23, 2020 at 8:40pm

GraphQL Playground signIn error

May 23, 2020 at 8:40pm
Receiving errors message: "data and hash arguments required" at Object.compare when I try to test: mutation{ signIn ( username email password ) }
I have no problem with signUp but for some reason that I can't figure out, it is giving me an error for signIn. When I run nodemon, it shows request as 'undefined'. Here is my code below:
signIn: async (parent, { username, email, password }, { models }) => { if (email) { // normalize email address email = email.trim().toLowerCase(); }
const user = await models.User.findOne({
$or: [{ email }, { username }]
});
// if no user is found, throw an authentication error
if (!user) {
throw new AuthenticationError('Error signing in');
}
// if the passwords don't match, throw an authentication error
const valid = await bcrypt.compare(password, user.password);
if (!valid) {
throw new AuthenticationError('Error signing in');
}
// create and return the json web token
return jwt.sign({ id: user._id }, process.env.JWT_SECRET);
},

May 24, 2020 at 2:41am
Hi, I just finished this chapter and run without problem. Are you assigning data to the fields?
mutation {
signIn(
username: "username",
password: "Test1234t"
)
}
  • reply
  • like

May 24, 2020 at 1:09pm
Yes. I added data.
  • reply
  • like
Hi ! Here is the full signIn mutation mutation, which looks the same as what you have posted:
signIn: async (parent, { username, email, password }, { models }) => {
if (email) {
// normalize email address
email = email.trim().toLowerCase();
}
const user = await models.User.findOne({
$or: [{ email }, { username }]
});
// if no user is found, throw an authentication error
if (!user) {
throw new AuthenticationError('Error signing in');
}
// if the passwords don't match, throw an authentication error
const valid = await bcrypt.compare(password, user.password);
if (!valid) {
throw new AuthenticationError('Error signing in');
}
// create and return the json web token
return jwt.sign({ id: user._id }, process.env.JWT_SECRET);
}
  • reply
  • like
I would also double check your user model to make sure it matches this https://github.com/javascripteverywhere/api/blob/master/solutions/05-Authentication/models/user.js
  • reply
  • like
Let me know if you're still running into any problems!
  • reply
  • like
Based on your error, it seems like it by might be bcrypt related which seems odd
  • reply
  • like

May 26, 2020 at 12:21am
I am also having the exact same issue. I also tried compareSync and adding the third parameter as shown in the Bcrypt documentation. Even the Solution folder does not seem to work in my system
  • reply
  • like
to help me troubleshoot - what OS are you using?
  • reply
  • like

May 26, 2020 at 4:22pm
I did some digging (aka consulting Stack Overflow) and it appears that the "data and hash arguments required" error means that "one or both of the first 2 parameters are either null or undefined."
and try putting a console.log statement just before the bcrypt check to see if either of those values is null or undefined:
console.log(password, user.password);
  • reply
  • like
to help me troubleshoot - what OS are you using?
Thank you for the prompt reply. I am using MacOS10.15.4. console.log gave me { id: '5ecd45e8f480280aa28e3583', iat: 1590511080 } password undefined
  • reply
  • like
Ok, that is super helpful. For some reason the password value is not being passed to the function. Can you take a close look at where that value should be coming from?
  • reply
  • like
Sure, I will work on that. I also think there is some issue with my version of Xcode CLT and node-gyp. BTW, Your book is awesome. Thank you very much.
like-fill
1
  • reply
  • like
I just figured out that I did not have correct database schema for user. Thank you for the support.
  • reply
  • like
Great to hear! Glad that you figured it out
  • reply
  • like
I'm still having the same issues. I tried to move on to the web portion and for some reason my .env is not connecting to my apollo client
  • reply
  • like

May 27, 2020 at 11:58pm
Hi , sorry to hear that you're still having trouble. A couple of questions: Are you using a git clone of the starter project? Have you run npm install? What command are you using the start the local web server?
  • reply
  • like