These are chat archives for FreeCodeCamp/HelpJavaScript

3rd
Feb 2018
Dovydas Stirpeika
@Giveback007
Feb 03 00:32
whoop whoop
JLuboff
@JLuboff
Feb 03 00:33
Thats the sound of the police
Dovydas Stirpeika
@Giveback007
Feb 03 00:33
@JLuboff :wave:
JLuboff
@JLuboff
Feb 03 00:33
Whats up
Dovydas Stirpeika
@Giveback007
Feb 03 00:34
not that many people asking for help in these rooms as there foor
JLuboff
@JLuboff
Feb 03 00:34
No.. :(
Dovydas Stirpeika
@Giveback007
Feb 03 00:34
before
JLuboff
@JLuboff
Feb 03 00:35
It is very slow unfortunately..
They may re-add the chat link when beta releases..but who knows
Dovydas Stirpeika
@Giveback007
Feb 03 00:36
im killing it at my new job
JLuboff
@JLuboff
Feb 03 00:36
Thats awesome
Dovydas Stirpeika
@Giveback007
Feb 03 00:37
:)
bragging feels nice
JLuboff
@JLuboff
Feb 03 00:38
Ya... well I've been doing some things too...so there :P
Dovydas Stirpeika
@Giveback007
Feb 03 00:38
yay :D
when is beta due to release
JLuboff
@JLuboff
Feb 03 00:40
Who knows. It was originally set for end of 2017 but they've delayed it further..
Dovydas Stirpeika
@Giveback007
Feb 03 00:41
bummer
JLuboff
@JLuboff
Feb 03 00:43
Ya...whatcha going to do
Mr.x
@learner75246
Feb 03 03:55
i need sb's help
how to correct this "var c= I am a"
FlashHero
@FlashHero
Feb 03 04:03
@learner75246 var c='I am a'; you need quotes around your strings
Mr.x
@learner75246
Feb 03 04:17
@FlashHero thanks
CamperBot
@camperbot
Feb 03 04:17
learner75246 sends brownie points to @flashhero :sparkles: :thumbsup: :sparkles:
:cookie: 319 | @flashhero |http://www.freecodecamp.org/flashhero
Mr.x
@learner75246
Feb 03 04:30
hello
abraham anak agung
@padunk
Feb 03 04:43
Afternoon
Mr.x
@learner75246
Feb 03 04:44
can i set this room to my basic chat room
abraham anak agung
@padunk
Feb 03 04:46
@learner75246 what do you mean ?
Mr.x
@learner75246
Feb 03 04:49
@padunk emm when i click the type "Chatroom",i wish into this Chatroom . what should i do?
abraham anak agung
@padunk
Feb 03 04:54
@learner75246 hmm, i don't quite understand what you mean by type. Do you open this chatroom in internet browser? just bookmark it then.
Mr.x
@learner75246
Feb 03 04:59
@padunk thank you
CamperBot
@camperbot
Feb 03 04:59
learner75246 sends brownie points to @padunk :sparkles: :thumbsup: :sparkles:
:cookie: 422 | @padunk |http://www.freecodecamp.org/padunk
Brad
@bradtaniguchi
Feb 03 06:46
Well I just get settled in, startup a webinar I've been watching to and get all the docs I want to skip thru, AND my laptop notifies me that its on low battery :/
and dangit this was suppose to be in the main chat oh well
Mr.x
@learner75246
Feb 03 08:59
what code can out put "I am "double quoted" string inside "double quotes" "
Razvan Jackson
@RazvanJackson
Feb 03 09:25
try ' "string" '
Sweet Coding :)
@SweetCodingInc
Feb 03 09:26
That's a double quoted string inside a single quoted string.
Razvan Jackson
@RazvanJackson
Feb 03 09:26
@learner75246 Or try smth like "I am \" double quoted \" string
Sweet Coding :)
@SweetCodingInc
Feb 03 09:26
he needs double quoted string inside a double quoted string
Razvan Jackson
@RazvanJackson
Feb 03 09:27
@learner75246 put \ before "
Sweet Coding :)
@SweetCodingInc
Feb 03 09:27
"I am \"double quoted\" string inside \"double quotes\" "
Razvan Jackson
@RazvanJackson
Feb 03 09:27
Someone here using React & Redux?
Sweet Coding :)
@SweetCodingInc
Feb 03 09:28
Yes
It's weekend and I have some free time.. Will checkout your repos @RazvanJackson
Razvan Jackson
@RazvanJackson
Feb 03 09:29
@SweetCodingInc Ok :)
function mapStateToProps(state){
    return {
        users: state.users
    }
}

export default connect(mapStateToProps)(UserList)
this thing it's getting all states from UserList and parse them to props?
Sweet Coding :)
@SweetCodingInc
Feb 03 09:31
It's the other way around
it's taking all the users from state and passing them on the user list component as props
Razvan Jackson
@RazvanJackson
Feb 03 09:33
@SweetCodingInc Damn this React and Redux so retarded ;)
Sweet Coding :)
@SweetCodingInc
Feb 03 09:33
Nah.. It's clean...
Razvan Jackson
@RazvanJackson
Feb 03 09:33
@SweetCodingInc Yesterday i learned some React and i made a shopping list it's in my repo
Sweet Coding :)
@SweetCodingInc
Feb 03 09:33
the name itself says "map state to props"
meaning you're matching properties from state object to props object
state to props
not props to state
@RazvanJackson Good!
Razvan Jackson
@RazvanJackson
Feb 03 09:36
Ayush Bahuguna
@relentless-coder
Feb 03 11:05
Hey, I am facing a rather weird problem. I have served pages via nodejs, and the problem is as I scroll up and down, the elements take a second to appear, it doesn't happen all the time, but let's 2 out of 5 times, this happens
Muhammad Hasham
@MohammadHasham
Feb 03 15:31
can anyone help me with mongodb?
Ogundele Olumide
@Lumexralph
Feb 03 15:38
@MohammadHasham what help do you need with MongoDB?
Muhammad Hasham
@MohammadHasham
Feb 03 15:41
a query actually
@Lumexralph
Muhammad Hasham
@MohammadHasham
Feb 03 17:14
any mongo expert here?
Sweet Coding :)
@SweetCodingInc
Feb 03 17:20
@MohammadHasham ask away your question already
Muhammad Hasham
@MohammadHasham
Feb 03 17:21
    User.find({_id:"5a737cba39452e6b18fd5ca9"},(err,doc)=>{
      console.log(doc.todos.0);
        res.send(doc.todos);
    });
i can't get the index of the array
it logs undefied
where as doc.todos is know
@SweetCodingInc
Sweet Coding :)
@SweetCodingInc
Feb 03 17:22
@MohammadHasham simple problem
Muhammad Hasham
@MohammadHasham
Feb 03 17:22
what is that?
Sweet Coding :)
@SweetCodingInc
Feb 03 17:22
not related to mongo though
Muhammad Hasham
@MohammadHasham
Feb 03 17:22
i have tried doc.todos[0]
Sweet Coding :)
@SweetCodingInc
Feb 03 17:22
should be console.log(doc.totos[0]);
Muhammad Hasham
@MohammadHasham
Feb 03 17:22
did that as well!
logs nothing
Sweet Coding :)
@SweetCodingInc
Feb 03 17:22
is there any error?
Muhammad Hasham
@MohammadHasham
Feb 03 17:22
undefined
Razvan Jackson
@RazvanJackson
Feb 03 17:22
maybe the id is wrong
Sweet Coding :)
@SweetCodingInc
Feb 03 17:22
have you tried console.log(err) ?
Muhammad Hasham
@MohammadHasham
Feb 03 17:22
yeah it gets me undefined
let me check
just a second
Sweet Coding :)
@SweetCodingInc
Feb 03 17:23
@RazvanJackson if id is wrong, the array will still be empty
Muhammad Hasham
@MohammadHasham
Feb 03 17:23
@SweetCodingInc the array is not empty
Sweet Coding :)
@SweetCodingInc
Feb 03 17:23
well then try doc[0]
Muhammad Hasham
@MohammadHasham
Feb 03 17:26
image.png
@SweetCodingInc
this is what i was doing
  app.post("/api/updateTodo", (req, res) => {
    //CR'Update'D
    User.find({_id:"5a737cba39452e6b18fd5ca9"},(err,doc)=>{
      console.log(doc[0].todos);
        res.send(doc.todos);
    });

  });
Sweet Coding :)
@SweetCodingInc
Feb 03 17:27
from what I see on the console screen shot, doc[0] has no property todos
you need to do doc[0].todo
Muhammad Hasham
@MohammadHasham
Feb 03 17:30
image.png
here is what i get on console.log(doc)
@SweetCodingInc
Sweet Coding :)
@SweetCodingInc
Feb 03 17:31
doc[0].todos should do then
Muhammad Hasham
@MohammadHasham
Feb 03 17:32
yeah now doc[0].todos gives me
image.png
@SweetCodingInc
but doc[0].todos[0] gives me undefined
Sweet Coding :)
@SweetCodingInc
Feb 03 17:33
that's strange
it should give you the first object
Muhammad Hasham
@MohammadHasham
Feb 03 17:33
yeah is there any work around
any other way i can accomplish this operation
@SweetCodingInc
Sweet Coding :)
@SweetCodingInc
Feb 03 17:35
not that I am aware of
Muhammad Hasham
@MohammadHasham
Feb 03 17:35
okay :-(
@SweetCodingInc thanks
CamperBot
@camperbot
Feb 03 17:35
mohammadhasham sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 226 | @sweetcodinginc |http://www.freecodecamp.org/sweetcodinginc
Razvan Jackson
@RazvanJackson
Feb 03 17:43
@SweetCodingInc
@SweetCodingInc can you help me with Redux?
I have this
this.props.dispatch({type:"CHANGE_FIRST_NAME"})
const userReducer = (state, action) => {
    state = [
        {
            name:'Razvan',
            age:17
        },
        {
            name:'Hanganu',
            age:17
        },
        {
            name:'Anca',
            age:16
        },
        {
            name:'Ana',
            age:17
        },
    ]    

    if(action.type === "CHANGE_FIRST_NAME") state[0].name = 'Foo';

    return state;
}
and here's the error
Maximum update depth exceeded
Sweet Coding :)
@SweetCodingInc
Feb 03 17:46
@RazvanJackson sure.. never mutate your state
@RazvanJackson give me a min
Razvan Jackson
@RazvanJackson
Feb 03 17:48
@SweetCodingInc Ok
Sweet Coding :)
@SweetCodingInc
Feb 03 17:51
@RazvanJackson Your reducer should look like this
const INITIAL_STATE = [
    {
        name:'Razvan',
        age:17
    },
    {
        name:'Hanganu',
        age:17
    },
    {
        name:'Anca',
        age:16
    },
    {
        name:'Ana',
        age:17
    },
];

const userReducer = (state = INITIAL_STATE, action) => {

    if(action.type === "CHANGE_FIRST_NAME") {
        const modifiedState = { ...state };
        modifiedState = modifiedState.map((s, i) => {
            if(i === 0){
                s.name = 'Foo';
            }
            return s;
        })
        return modifiedState;
    }

    return { ...state };
}
Razvan Jackson
@RazvanJackson
Feb 03 17:52
@SweetCodingInc OMG Pain in the...
Pieter Stokkink
@forkerino
Feb 03 17:53
@SweetCodingInc I see a small error, you're .map ing an object
also there's not really a need to make a copy, since you are using .map, which returns a new array anyway.
Razvan Jackson
@RazvanJackson
Feb 03 17:53
@forkerino there's an array of obj
Pieter Stokkink
@forkerino
Feb 03 17:54
yeah, I'm aware of that
state is an array
but { ...state } turns it into an object
{...[1,2,3]} // {0: 1, 1: 2, 2: 3}
Sweet Coding :)
@SweetCodingInc
Feb 03 18:03
Sorry.. my bad... @forkerino is right
it should be const modifiedState = [ ...state ];
not const modifiedState = { ...state };

also there's not really a need to make a copy, since you are using .map, which returns a new array anyway.

matter of standard practice that's all... It's a guarding mechanism to proactively prevent accidental state mutation

Sweet Coding :)
@SweetCodingInc
Feb 03 18:11
@RazvanJackson
const INITIAL_STATE = [
    {
        name:'Razvan',
        age:17
    },
    {
        name:'Hanganu',
        age:17
    },
    {
        name:'Anca',
        age:16
    },
    {
        name:'Ana',
        age:17
    },
];

const userReducer = (state = INITIAL_STATE, action) => {

    if(action.type === "CHANGE_FIRST_NAME") {
        const modifiedState = [ ...state ];
        modifiedState = modifiedState.map((s, i) => {
            if(i === 0){
                s.name = 'Foo';
            }
            return s;
        })
        return modifiedState;
    }

    return [ ...state ];
}
Razvan Jackson
@RazvanJackson
Feb 03 18:12
@SweetCodingInc Not working, however i got the problem :) I'll solve it tommorrow :)
@SweetCodingInc Let's give you a cookie for helping. Thanks.
CamperBot
@camperbot
Feb 03 18:12
razvanjackson sends brownie points to @sweetcodinginc :sparkles: :thumbsup: :sparkles:
:cookie: 227 | @sweetcodinginc |http://www.freecodecamp.org/sweetcodinginc
Pieter Stokkink
@forkerino
Feb 03 18:12
@SweetCodingInc Yeah, it's good to have such a guarding mechanism. We're using Redux in production for our app and never really bother with always copying state before returning something.
But I guess it can prevent a lot of bugs
Sweet Coding :)
@SweetCodingInc
Feb 03 18:15
@forkerino I ran into that issue many times with both redux and ngrx/store (for Angular)
Most of the times the problem isn't apparent enough to be noticed. What happens is, it ends up triggering multiple listeners should you change a single property
So I made it a habit to never touch state directly
@RazvanJackson what do you mean not working :laughing:
Pieter Stokkink
@forkerino
Feb 03 18:17
yeah, we only use objects and have things like this in our reducers:
return {
  ...state,
  thePropWeChange: 'newValue'
}
Sweet Coding :)
@SweetCodingInc
Feb 03 18:18
That's a correct way...
in the example above the state is an array directly
one of the reason I habitually wrote {...state} instead of [...state]
:laughing:
Pieter Stokkink
@forkerino
Feb 03 18:19
yes
arrays are a bit harder to reason about, I guess, because you'd have to know which methods return a new array and which modify it.
Sweet Coding :)
@SweetCodingInc
Feb 03 18:20
yeah.. and it's never a realistic scenario that your entire state is gonna be just one array.
Pieter Stokkink
@forkerino
Feb 03 18:20
true
Sweet Coding :)
@SweetCodingInc
Feb 03 18:22
You'd end up with data from multiple source and you'd have multiple selectors for different sources
and different reducers as well
FlashHero
@FlashHero
Feb 03 22:06
class Routes extends React.Component{
    render(){
        return(
          <Route exact path='/home' component={App}/>
        )
    }
}
Why does this Route work I thought you needed <Router> for it to work. And when creating routes, do you have to put them inside render's return
alpox
@alpox
Feb 03 22:22
@FlashHero does any parent component contain a router? :)
And yes routes can only be jsx elements which get returned from renders