๐Ÿ‘ฉ‍๐Ÿ’ป/Firebase

[firebase] updateProfile is not a function Issue

ํ•œ๋‚˜ 2021. 3. 14. 15:00

 

๋ฌธ์ œ ์ƒํ™ฉ

Firebase์˜ Auth ๊ธฐ๋Šฅ์„ ์ด์šฉํ•ด ์œ ์ €์˜ ํ”„๋กœํ•„ ์ •๋ณด๋ฅผ ๋ฐ›์•„์„œ DisplayName์„ ์—…๋ฐ์ดํŠธํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์—…๋ฐ์ดํŠธ๋Š” ์ œ๋Œ€๋กœ ๋์ง€๋งŒ, ๋‘ ๋ฒˆ์งธ๋ถ€ํ„ฐ๋Š” ์•„๋ž˜ ๊ฐ™์€ ์—๋Ÿฌ๋ฅผ ๋ƒˆ๋‹ค.

 

userObj.updateProfile is not a function

๋ฌธ์ œ ํ•ด๊ฒฐ

์ตœ์ƒ์œ„ App.js ํŒŒ์ผ์—์„œ refreshuser๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋‘๊ณ , ํ”„๋กœํ•„ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๋งˆ๋‹ค ์ด๋ฅผ ๋ฐ”๋กœ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ firebase.auth๋กœ๋ถ€ํ„ฐ currentUser๋ฅผ ๋ฐ›์•„์˜ค๊ณ  ์žˆ์—ˆ๋‹ค. ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์ง€๋งŒ, ์ด๋ฅผ Object.assign({}, user))๋กœ ๋ณต์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ณผ์ •์—์„œ enumerable ์†์„ฑ์ธ ๋ฉ”์†Œ๋“œ updateProfile์„ ๋ณต์‚ฌํ•ด์˜ค์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์œ ์ €๊ฐ€ ํ”„๋กœํ•„ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•œ ์ฒซ ๋ฒˆ์งธ ์‹œ์ ์—์„œ๋Š” ์ œ๋Œ€๋กœ ์ž‘๋™ํ–ˆ์ง€๋งŒ, refreshuser ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ์ดํ›„๋ถ€ํ„ฐ๋Š” UserObject๋กœ๋ถ€ํ„ฐ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค.

์ฝ”๋“œ

// App.js
import { autService } from 'fBase';

function App() {
  const [userObj, setUserObj] = useState(null);
  // ....

  const refreshuser = () => {
      const user = authService.currentUser;
    // setUserObj(Object.assign({}, user)); (*)
    setUserObj({
            displayName: user.displayName,
            uid: user.uid,
            updateProfile: (args) => user.updateProfile(args)
    });
  }
}

refreshuser๋Š” prop์œผ๋กœ ์ „๋‹ฌ๋˜์–ด์„œ, Profile.js์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์“ฐ์ธ๋‹ค.

// Profile.js


    const onSubmit = async (e) => { // profile update๋ฅผ ์œ„ํ•œ Form์— onSubmit์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„
        e.preventDefault();
        if (newDisplayName === '') {
            setError('Name can\'t be empty');
        }
        if (userObj.displayName !== newDisplayName) {
            try {
                await userObj.updateProfile({
                    displayName: newDisplayName
                });
                refreshuser(); // refreshuser๊ฐ€ ์“ฐ์ด๋Š” ๋ถ€๋ถ„
            } catch (err) {
                console.log(err)
            }
        }

    }

Object.assign()

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.

from MDN