[英]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 个参数:
and it is called from two locations:它从两个位置调用:
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,} Lingoaction @@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.