user10728848
user10728848

Reputation: 133

Firebase: TypeError: fbRef.child is not a function

I'm trying to use firebase for the first time with a web-applicaiton, but I get this error about .child is not a function. Can anyone please show the right way to connect to the firebase database collection, if what i am doing is wrong.

If I prefer to use mongoDb instead of firebase, how should I implement my post method in this case?

This is the error I get upon posting the form on the site.

TypeError: fbRef.child is not a function

at C:\Users\WorkoutApp_v1\routes\workouts.js:32:23
at Layer.handle [as handle_request] (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\WorkoutApp_v1\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\WorkoutApp_v1\node_modules\express\lib\router\index.js:47:12)

This is the post method in my router file.

var express = require('express');
var router = express.Router();
const Firebase = require('firebase');
const fbRef = 'https://console.firebase.google.com/project/workout-7f912/database/firestore  /data~2Fexercise~2F2zCvky1fWQcD14tTCq8B';

router.post('/add', function(req,res,next) {
var workout = {
    name: req.body.Name,
    discription: req.body.Discription,
    set: req.body.Set,
    RepsTime: req.body.RepsTime
}
// console.log(workout);
// return;

var dbRef = fbRef.child('workouts');
dbRef.push().set(workout);

req.flash('success_msg', 'Workout saved');
res.redirect('/workouts');
});

Upvotes: 1

Views: 341

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

fbRef is a string and does not contain a method called child. You need to first connect to firebase database then you can do the following:

const fbRef = firebase.database().ref();
var dbRef = fbRef.child('workouts');

For more information check the following:

https://firebase.google.com/docs/database/web/start

Upvotes: 1

Related Questions