''TypeError: coment.map is not a function' with realtime database of firebase
I wrote this code to receive all comments from the database and then print them one by one in the console. The code is:
import {
getDatabase,
ref,
child,
get,
} from "https://www.gstatic.com/firebasejs/9.8.0/firebase-database.js";
const dbRef = ref(getDatabase());
get(child(dbRef, `comentarios/`))
.then((snapshot) => {
if (snapshot.exists()) {
//Get an array of objects
var coment = snapshot.val();
console.log(coment)
//Transforms array of objects into normal array
const commentsExtracted = coment.map((coment) => {
return coment.coment;
});
//Print the comments to the console
for (let i = 0; i < commentsExtracted.length; i++) {
if (
commentsExtracted[i] != undefined ||
commentsExtracted[i] == ""
) {
console.log(commentsExtracted[i]);
}
}
} else {
console.log("No data available");
}
})
.catch((error) => {
console.error(error);
});
the error that is shown in the console is:
comentarios.js:37 TypeError: coment.map is not a function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|