title: Javascript: Passing valid parameters in function call and one turns undefined
body: <p>It's a React web app with Redux and Firebase. I've been finishing implementation of react-redux-i18n (multilingual support) when I hit this problem. 
  The app supports two locales, let's say 'en' and 'pl'. Current locale is stored in state and in sync with firebase, that's the idea anyway and that's where I've encountered this strange behaviour: one of the 3 parameters, while present and valid before the call to the function turns undefined in the function that subsequently fails.</p>
  
  <p>Here is the function I'm calling:</p>
  
  <pre><code>export const SaveLanguageToDb = (uid, user, lang) =&gt; {
    console.log('in function:', uid, user)
    database.ref(`users/${uid}`).update({ currentLanguage: lang, ...user })
    .then(() =&gt; {
      return
    })
    .catch(e =&gt; { console.log('Error:', e) })
  }
  </code></pre>
  
  <p>The function takes 3 parameters:</p>
  
  <ul>
  <li>uid: string, </li>
  <li>user: object,</li>
  <li>lang: string</li>
  </ul>
  
  <p>and it is called from two locations:</p>
  
  <ol>
  <li>On app load it gets user data incl. locale from firebase and saves
  it back to firebase after validation in case a locale was not
  supported and has fallen back to 'en'. It's possible if support was
  removed for locale that user has stored previously. This call works
  correctly. </li>
  <li>From the language changer component when user clicks on a flag pic
  to change locale. This call fails due to <code>user</code> object coming in
  undefined.</li>
  </ol>
  
  <p>Here is the component the function is called from:</p>
  
  <pre><code>import React from 'react'
  import { connect } from 'react-redux'
  import en from '../../../public/images/United_Kingdom.png'
  import pl from '../../../public/images/Poland.png'
  import { SaveLanguageToDb } from '../../actions/user'
  import { setLocale, } from 'react-redux-i18n'
  
  export const LingoFlags = ({ uid, user, SaveLanguageToDb, setLocale }) =&gt; {
    console.log('before call:', uid, user)
    return (
    &lt;div&gt;
      &lt;button
        className="button button--link"  
        onClick={() =&gt; {
          setLocale('en')
          SaveLanguageToDb(uid, user, 'en')
        }}
      &gt;
        &lt;img src={en} alt="en" /&gt;
      &lt;/button&gt;
      &lt;button
        className="button button--link"  
        onClick={() =&gt; {
          console.log('onClick:', uid, user)
          setLocale('pl')
          SaveLanguageToDb(uid, user, 'pl')
        }}
      &gt;
        &lt;img src={pl} alt="pl" /&gt;
      &lt;/button&gt;
    &lt;/div&gt;
  )}
  
  const matchStateToProps = (state) =&gt; ({
    uid: state.auth.uid,
    user: state.user,
  })
  
  const mapDispatchToProps = (dispatch) =&gt; ({
    SaveLanguageToDb: (lang) =&gt; dispatch(SaveLanguageToDb(lang)),
    setLocale: (lang) =&gt; dispatch(setLocale(lang))
  }) 
  
  export default connect(matchStateToProps, mapDispatchToProps)(LingoFlags)
  </code></pre>
  
  <p>The <code>console.log</code>s confirm that I have the correct data just before the call and yet in the called function the <code>user</code> object is undefined while <code>uid</code> string is passed correctly as shown below: </p>
  
  <blockquote>
    <p><strong>before call</strong>: 06N6iv34gZfyWeF {displayName: "Some Name", email: "somenamel@gmail.com", emailVerified: true, numberOfVisits: 102,
    firstVisitAt: 1591402705798, …} LingoFlags.js:9<br>
    <strong>onClick</strong>: 06N6iv34gZfyWeF {displayName: "Some Name", email: "somename@gmail.com", emailVerified: true, numberOfVisits: 102,
    firstVisitAt: 1591402705798, …} LingoFlags.js:24  </p>
    
    <p>action @@i18n/SET_LOCALE @ 19:13:40.651 redux-logger.js:1</p>
    
    <p><strong>in function</strong>: 06N6iv34gZfyWeF <strong>undefined</strong> user.js:41</p>
    
    <p>Uncaught Error: Reference.update failed: First argument contains
    undefined...</p>
  </blockquote>
  
  <p>I hope that is enough info for someone to get interested in this mystery. Thank you.</p>