hidjou / classsed-react-firebase-functions

327 stars 180 forks source link

TypeError: Cannot read property 'trim' of undefined > at isEmpty #17

Open MatthieuTnsc opened 4 years ago

MatthieuTnsc commented 4 years ago

Hi, I am following the YouTube tutorial (amazing job 👍)

I am at the stage of building the signup and login validation functions. When I POST a signup request with an empty value I've got an TypeError due to the trim function used in the 'isEmtpy' helper.

Any idea how to fix it?

Thanks

>  TypeError: Cannot read property 'trim' of undefined
>      at isEmpty (/Users/***/***/***/***/functions/index.js:69:14)
>      at /Users/***/***/***/***/functions/index.js:93:7
const isEmpty = (string) => {
  if (string.trim() === '') return true;
  else return false;
};
fernandovch commented 4 years ago

@MatthieuTnsc did you solve it? I'm getting the same error. My questions is that I should validate if (req.body.body.trim() === '') {
return res.status(400).json({ body: 'Body must not be empty' }); } on the post for creating screams.

MatthieuTnsc commented 4 years ago

Hi @fernandovch no I didn't. I had the same behavior each time I had to use the .trim(). I removed it from the validation functions to keep on moving with the tutorial..

davigsa commented 4 years ago

Are you sure that string isnt returning as undefined?. Sometimes, this error is caused by this. Try to prevent this error from occurring.

TurboJapuraEfac commented 4 years ago

I tried so many different options for 3 days. Did not get the correct answer

thilanka commented 4 years ago

I also got the same error at some point. But I solved it. The type undefined means the string is not getting a value when it's been called. When it's been called from login make sure the valid data is passed. Sometimes the variables aren't assigned properly. upload you repo here on github maybe i can help.

TurboJapuraEfac commented 4 years ago

Hi Thilanka, this is the code for adding user details

exports.addUserDetails = (req, res) => { let userDetails = reduceUserDetails(req.body); db.doc(/users/${req.user.handle}).update(userDetails) .then(() => { return res.json({message: 'Details added successfully'}) }) .catch(err => { console.error(err); return res.status(500).json({error: err.code}) }); } and this is the code for reducing data exports.reduceUserDetails = (data) => { let userDetails = {}; if(!isEmpty(data.bio.trim())) userDetails.bio = data.bio; if(!isEmpty(data.location.trim())) userDetails.location = data.location; return userDetails; }

thilanka commented 4 years ago

Hi Thilanka, this is the code for adding user details

exports.addUserDetails = (req, res) => { let userDetails = reduceUserDetails(req.body); db.doc(/users/${req.user.handle}).update(userDetails) .then(() => { return res.json({message: 'Details added successfully'}) }) .catch(err => { console.error(err); return res.status(500).json({error: err.code}) }); } and this is the code for reducing data exports.reduceUserDetails = (data) => { let userDetails = {}; if(!isEmpty(data.bio.trim())) userDetails.bio = data.bio; if(!isEmpty(data.location.trim())) userDetails.location = data.location; return userDetails; }

could you put the console log as well? also if it's possible upload yourcode here on github as a repo. install git then change in to code folder.

git init
git add .
git commit -m "Initial commit"
git remote rename origin old-origin
git remote add origin https://github.com/your-github-username/your-git-repo.git
git push -u origin --all
TurboJapuraEfac commented 4 years ago

https://github.com/TurboJapuraEfac/FirebaseBackend This is the repository. Add user information is the part that is involved with this issue.

TypeError: Cannot read property 'trim' of undefined at exports.reduceUserDetails

This is how I passed data from postman to test whether data is added { "bio" : "Kumar Sangakkara ", "website" : "user.com", "location " : "London, UK" }

thilanka commented 4 years ago

Check file validators.js line 43 and 44.

const userDetails = {};

  if (!isEmpty(data.bio.trim())) userDetails.bio = data.bio;
  if (!isEmpty(data.website.trim())) {

you are sending a string which is already trimmed.

const isEmpty = (string) => {
    if (string.trim() === '') return true;
    else return false;
};
TurboJapuraEfac commented 4 years ago

Thank you Thilanka. It was a error when passing the data from PostMan. Thank you so much. I ran the server locally and found the error.

thilanka commented 4 years ago

I deployed your project to firebase and was just trying out the function. Glad you were able to find out the error. I finished part 9 of the youtube series a few minutes ago.

On Thu, Jun 25, 2020 at 10:08 PM Buddhika Weerasinghe < notifications@github.com> wrote:

Thank you Thilanka. It was a error when passing the data from PostMan. Thank you so much. I ran the server locally and found the error.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/hidjou/classsed-react-firebase-functions/issues/17#issuecomment-649679446, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAQBIJG77ZELZLFFCH7ZXTRYN4O7ANCNFSM4NC3W3KA .

--