简体   繁体   English

Javascript:在 function 调用中传递有效参数,一转未定义

[英]Javascript: Passing valid parameters in function call and one turns undefined

It's a React web app with Redux and Firebase.这是一个带有 Redux 和 Firebase 的 React web 应用程序。 I've been finishing implementation of react-redux-i18n (multilingual support) when I hit this problem.当我遇到这个问题时,我已经完成了 react-redux-i18n(多语言支持)的实现。 The app supports two locales, let's say 'en' and 'pl'.该应用程序支持两种语言环境,比如说“en”和“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. 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失败。

Here is the function I'm calling:这是我打电话的 function:

export const SaveLanguageToDb = (uid, user, lang) => {
  console.log('in function:', uid, user)
  database.ref(`users/${uid}`).update({ currentLanguage: lang, ...user })
  .then(() => {
    return
  })
  .catch(e => { console.log('Error:', e) })
}

The function takes 3 parameters: function 采用 3 个参数:

  • uid: string, uid:字符串,
  • user: object,用户:object,
  • lang: string语言:字符串

and it is called from two locations:它从两个位置调用:

  1. 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'.来自 firebase 的语言环境,并在验证后将其保存回 firebase,以防不支持语言环境并回退到“en”。 It's possible if support was removed for locale that user has stored previously.如果删除了对用户先前存储的语言环境的支持,则有可能。 This call works correctly.此调用正常工作。
  2. From the language changer component when user clicks on a flag pic to change locale.当用户单击标志图片以更改语言环境时,来自语言更改器组件。 This call fails due to user object coming in undefined.由于user object 未定义,此调用失败。

Here is the component the function is called from:这是 function 的调用组件:

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 }) => {
  console.log('before call:', uid, user)
  return (
  <div>
    <button
      className="button button--link"  
      onClick={() => {
        setLocale('en')
        SaveLanguageToDb(uid, user, 'en')
      }}
    >
      <img src={en} alt="en" />
    </button>
    <button
      className="button button--link"  
      onClick={() => {
        console.log('onClick:', uid, user)
        setLocale('pl')
        SaveLanguageToDb(uid, user, 'pl')
      }}
    >
      <img src={pl} alt="pl" />
    </button>
  </div>
)}

const matchStateToProps = (state) => ({
  uid: state.auth.uid,
  user: state.user,
})

const mapDispatchToProps = (dispatch) => ({
  SaveLanguageToDb: (lang) => dispatch(SaveLanguageToDb(lang)),
  setLocale: (lang) => dispatch(setLocale(lang))
}) 

export default connect(matchStateToProps, mapDispatchToProps)(LingoFlags)

The console.log s confirm that I have the correct data just before the call and yet in the called function the user object is undefined while uid string is passed correctly as shown below: console.log确认我在调用之前有正确的数据,但在被调用的 function 中, user object 未定义,而uid字符串正确传递,如下所示:

before call : 06N6iv34gZfyWeF {displayName: "Some Name", email: "somenamel@gmail.com", emailVerified: true, numberOfVisits: 102, firstVisitAt: 1591402705798, …} LingoFlags.js:9通话前:06N6iv34gZfyWeF {displayName: "Some Name", email: "somenamel@gmail.com", emailVerified: true, numberOfVisits: 102, firstVisitAt: 1591402705798, ...} LingoFlags.js:9
onClick : 06N6iv34gZfyWeF {displayName: "Some Name", email: "somename@gmail.com", emailVerified: true, numberOfVisits: 102, firstVisitAt: 1591402705798, …} LingoFlags.js:24 onClick : 06N6iv34gZfyWeF {displayName: "Some Name", email: "somename@gmail.com", emailVerified: true, numberOfVisits: 102, firstVisit7070:2, ...} Lingo70570:28,} Lingo

action @@i18n/SET_LOCALE @ 19:13:40.651 redux-logger.js:1动作@@i18n/SET_LOCALE@19:13:40.651 redux-logger.js:1

in function : 06N6iv34gZfyWeF undefined user.js:41在 function 中:06N6iv34gZfyWeF未定义user.js:41

Uncaught Error: Reference.update failed: First argument contains undefined...未捕获的错误:Reference.update 失败:第一个参数包含未定义...

I hope that is enough info for someone to get interested in this mystery.我希望这些信息足以让人们对这个谜团感兴趣。 Thank you.谢谢你。

According to the mapDispatchToProps 'SaveLanguageToDb' receives only 1 argument.根据 mapDispatchToProps 'SaveLanguageToDb' 仅接收 1 个参数。 Is that might be the issue?这可能是问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM