如何解决这个非常奇怪的问题与google firebase试图获得变量值?

时间:2017-04-25 19:48:08

标签: javascript firebase firebase-realtime-database

public\index.html的内容如下:

<script src="https://(...firebase link...)"></script>
<script src="three.js"></script><script>
document.write("<div id='d'></div>")
document.body.style.backgroundColor="black"
document.body.style.color="white"
document.body.style.margin="0 0 0 0"
firebase.initializeApp({...})
//Read: net.database().ref('var').on('value',function(e){e.val()})
//Write: net.database().ref('var').set("val")
document.title="A survival p2p game."
document.write(".")
document.body.innerHTML=""
document.body.style.margin="0 0 0 0"
document.body.style.backgroundColor="black"
scene=new THREE.Scene()
camera=new THREE.PerspectiveCamera(75,1,1,10000)
renderer=new THREE.WebGLRenderer()
document.body.appendChild(renderer.domElement)
cube=new THREE.Mesh(new THREE.BoxGeometry(1,1,1),new THREE.MeshBasicMaterial({color:"rgb(0,255,0)"}))
scene.add(cube)
camera.position.z=5
firebase.database().ref('playerx').set(0)
firebase.database().ref('playery').set(0)
firebase.database().ref('playerz').set(5)
firebase.database().ref('playerx').on('value',function(e){cube.postion.x=e.val()})
firebase.database().ref('playery').on('value',function(e){cube.postion.y=e.val()})
firebase.database().ref('playerz').on('value',function(e){cube.postion.z=e.val()})
function render(){
camera.aspect=window.innerWidth/window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(innerWidth,innerHeight)
requestAnimationFrame(render)
renderer.render(scene,camera)
}
render()
</script>

错误就是这些:

>FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot set property 'x' of undefined
--links--
J @ firebase-app.js:1677
(anonymous) @ firebase-app.js:1677
firebase-app.js:1677 Uncaught TypeError: Cannot set property 'x' of undefined
    at (index):31
    at firebase-app.js:1677
    at fc (firebase-app.js:1677)
    at bf (firebase-app.js:1677)
    at af (firebase-app.js:1677)
    at Fh (firebase-app.js:1677)
    at U.g.gc (firebase-app.js:1677)
    at (index):31
(anonymous) @ (index):31
(anonymous) @ firebase-app.js:1677
fc @ firebase-app.js:1677
bf @ firebase-app.js:1677
af @ firebase-app.js:1677
Fh @ firebase-app.js:1677
g.gc @ firebase-app.js:1677
(anonymous) @ (index):31

>firebase-app.js:1677 FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot set property 'y' of undefined
J @ firebase-app.js:1677
(anonymous) @ firebase-app.js:1677
firebase-app.js:1677 Uncaught TypeError: Cannot set property 'y' of undefined
--links--
    at (index):32
    at firebase-app.js:1677
    at fc (firebase-app.js:1677)
    at bf (firebase-app.js:1677)
    at af (firebase-app.js:1677)
    at Fh (firebase-app.js:1677)
    at U.g.gc (firebase-app.js:1677)
    at (index):32
(anonymous) @ (index):32
(anonymous) @ firebase-app.js:1677
fc @ firebase-app.js:1677
bf @ firebase-app.js:1677
af @ firebase-app.js:1677
Fh @ firebase-app.js:1677
g.gc @ firebase-app.js:1677
(anonymous) @ (index):32
>firebase-app.js:1677 FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot set property 'z' of undefined
J @ firebase-app.js:1677
(anonymous) @ firebase-app.js:1677
firebase-app.js:1677 Uncaught TypeError: Cannot set property 'z' of undefined
--links--
at (index):33
at firebase-app.js:1677
at fc (firebase-app.js:1677)
at bf (firebase-app.js:1677)
at af (firebase-app.js:1677)
at Fh (firebase-app.js:1677)
at U.g.gc (firebase-app.js:1677)
at (index):33
(anonymous) @ (index):33
(anonymous) @ firebase-app.js:1677
fc @ firebase-app.js:1677
bf @ firebase-app.js:1677
af @ firebase-app.js:1677
Fh @ firebase-app.js:1677
g.gc @ firebase-app.js:1677
(anonymous) @ (index):33

是的,我想得到playerx变量,但是由于这个奇怪的错误,我无法做任何逻辑。我想改变:

firebase.initializeApp({ apiKey:"...", authDomain:"zurbibalguame123.firebaseapp.com", databaseURL:"https://zurbibalguame123.firebaseio.com", projectId:"zurbibalguame123", storageBucket:"zurbibalguame123.appspot.com", messagingSenderId:"..."})

为:

net=firebase.initializeApp({ apiKey:"...", authDomain:"zurbibalguame123.firebaseapp.com", databaseURL:"https://zurbibalguame123.firebaseio.com", projectId:"zurbibalguame123", storageBucket:"zurbibalguame123.appspot.com", messagingSenderId:"..."})

使用net代替firebase,但会发生相同的错误。

2 个答案:

答案 0 :(得分:1)

你有一个错字。 cube.postion应该是cube.position

答案 1 :(得分:0)

很抱歉给我带来不便,因为我的失明,但更正的代码在这里:

<script src="https://www.gstatic.com/firebasejs/3.8.0/firebase.js"></script>
<script src="three.js"></script><script>
document.write("<div id='d'></div>")
document.body.style.backgroundColor="black"
document.body.style.color="white"
document.body.style.margin="0 0 0 0"
db=firebase.initializeApp({...})
//Read: net.database().ref('var').on('value',function(e){e.val()})
//Write: net.database().ref('var').set("val")
document.title="A survival p2p game."
document.write(".")
document.body.innerHTML=""
document.body.style.margin="0 0 0 0"
document.body.style.backgroundColor="black"
scene=new THREE.Scene()
camera=new THREE.PerspectiveCamera(75,1,1,10000)
renderer=new THREE.WebGLRenderer()
document.body.appendChild(renderer.domElement)
cube=new THREE.Mesh(new THREE.BoxGeometry(1,1,1),new THREE.MeshBasicMaterial({color:"rgb(0,255,0)"}))
scene.add(cube)
camera.position.z=5
db.database().ref('playerx').set(0)
db.database().ref('playery').set(0)
db.database().ref('playerz').set(-5)
db.database().ref('playerx').on('value',function(e){cube.position.x=e.val()})
db.database().ref('playery').on('value',function(e){cube.position.y=e.val()})
db.database().ref('playerz').on('value',function(e){cube.position.z=e.val()})
function render(){
camera.aspect=window.innerWidth/window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(innerWidth,innerHeight)
requestAnimationFrame(render)
renderer.render(scene,camera)
}
render()
</script>