javascript - Cannot read property 'ref' of undefined - Stack Overflow

I'm building a simple web app with Vue + Firebase + Vuefire and I get "Uncaught TypeError: Ca

I'm building a simple web app with Vue + Firebase + Vuefire and I get "Uncaught TypeError: Cannot read property 'ref' of undefined" when I try to use my Firebase db variable inside a ponent.

In main.js

Vue.use(VueFire)

const firebaseApp = Firebase.initializeApp({ //setting })

// Export the database for ponents to use.
export const db = firebaseApp.database()

And in my ponent

// in Recipes.vue
import {db} from '../main.js'

export default {
  recipe: {
    source: db.ref('recipes')
    // Console says: "Uncaught TypeError: Cannot read property 'ref' of undefined"
  }
}

I followed the steps in this tutorial /

This code db.ref('recipes') works if used inside main.js, but it never works once I import it inside my ponent.

I'm building a simple web app with Vue + Firebase + Vuefire and I get "Uncaught TypeError: Cannot read property 'ref' of undefined" when I try to use my Firebase db variable inside a ponent.

In main.js

Vue.use(VueFire)

const firebaseApp = Firebase.initializeApp({ //setting })

// Export the database for ponents to use.
export const db = firebaseApp.database()

And in my ponent

// in Recipes.vue
import {db} from '../main.js'

export default {
  recipe: {
    source: db.ref('recipes')
    // Console says: "Uncaught TypeError: Cannot read property 'ref' of undefined"
  }
}

I followed the steps in this tutorial https://alligator.io/vuejs/vuefire-firebase/

This code db.ref('recipes') works if used inside main.js, but it never works once I import it inside my ponent.

Share Improve this question edited Nov 12, 2017 at 21:03 Fabrunet asked Nov 12, 2017 at 21:02 FabrunetFabrunet 691 gold badge1 silver badge7 bronze badges 2
  • The immediate difference I see between your code and the tutorial is that you're including the .js extension when importing. Perhaps you should try omitting that and instead use import {db} from '../main'. Granted, I've never used the importing or exporting, so I can't say whether or not this will remedy your problem. – B. Fleming Commented Nov 13, 2017 at 1:25
  • Good observation. I never noticed it could work without the .js extension. I gave it a try, the path still works, but I have the same error. – Fabrunet Commented Nov 13, 2017 at 3:31
Add a ment  | 

2 Answers 2

Reset to default 2

The problem was my Firebase code (including db variable) was inside main.js but it needed to be in it's own ponent. I created a firebase.js file :

import Firebase from 'firebase'

const firebaseApp = Firebase.initializeApp({
  # configuration
})

// Export the database for ponents to use.
export const db = firebaseApp.database()

Then in my ponent I simply imported my database variable :

import {db} from '../firebase'

Why didn't it work inside main.js? I'm not sure, maybe someone more knowledgeable can answer that.

.ref is a firebase function, you need to import it. try

import Firebase from 'firebase'

or

import * from 'firebase'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744699701a4588705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信