Estoy tratando de usar las funciones de la nube de firebase en el proyecto Next.js, pero hay un error que no sé cómo solucionar.
firebase-config.js
const firebaseConfig = { apiKey: '~~~', authDomain: '~~', projectId: '~~~', storageBucket: '~~~', messagingSenderId: '~~~~~', appId: '~~~~~', measurementId: '~~~~', }; const app = initializeApp(firebaseConfig); const db = getDatabase(app); const auth = getAuth(app); const functions = getFunctions(app); let analytics = null; if (app.name && typeof window !== 'undefined') { analytics = getAnalytics(app); } export { db, auth, functions, analytics }; Implementé la función de nube addMessage en mi base de fuego y llamé a esta función dentro de la /pages/friends
import React, { useEffect, useState } from 'react' import { doc, getDoc } from 'firebase/firestore'; import { auth, functions } from '../../firebase-config'; import { getAuth, onAuthStateChanged, signOut } from 'firebase/auth'; const addMessage = functions.httpsCallable('addMessage'); export default function FriendsPage() { const [user, setUser] = useState(null); const [friendsList, setFriendsList] = useState(null); const currUser = auth.currentUser; const _onAuthStateChanged = (handler) => { if(!auth) return; onAuthStateChanged(auth, (user) => { //TODO(aaron) : bug fix if (user) { const uid = user.uid; console.log('AuthStateChanged', uid); //user is signed in handler(user); } else { // user is signed out } }); }; const addFriend = () => { addMessage().then((res)=>{ console.log(res); }) } useEffect(()=>{ setFriendsList() _onAuthStateChanged(setUser); console.log(user); }, []); if(!user){ return( <p>Loading...</p> ) } return ( <div> <h3>{user.first}s friends List</h3> <ul> <li>h</li> <li>h</li> <li>h</li> </ul> <buton onClick={addFriend}>Call functions</buton> </div> ) }Pero aparece este mensaje de error. Creo que probablemente sea un problema cuando se cargan las funciones, ¿cómo puedo resolver esto?
Server Error TypeError: _firebase_config__WEBPACK_IMPORTED_MODULE_2__.functions.httpsCallable is not a function This error happened while generating the page. Any console logs will be displayed in the terminal window. Source pages/friends/index.js (6:19) @ eval 4 | import { getAuth, onAuthStateChanged, signOut } from 'firebase/auth'; 5 | > 6 | const addMessage = functions.httpsCallable('addMessage'); | ^ 7 | 8 | export default function FriendsPage() { 9 | const [user, setUser] = useState(null);Estás mezclando la sintaxis v8 y v9 para las funciones invocables. Parece que está usando la v9 del SDK en su aplicación, no la v8. Cuando importa getFunctions, no obtiene un objeto que tenga un método llamado httpsCallable. Eso es lo que el mensaje de error intenta decirte. En su lugar, debe importar la función httpsCallable y pasarle el parámetro de función como argumento. Vea el ejemplo en la documentación para v9 .
import { getFunctions, httpsCallable } from "firebase/functions"; const functions = getFunctions(); const addMessage = httpsCallable(functions, 'addMessage');