You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

远程灯光控制项目中Firebase数据更新失败与firebase serve运行报错的问题求助

Hey there! Let's break down your issues one by one and get your remote light control project up and running smoothly.

Issue 1: Can't update Firebase Realtime Database values

Let's tackle the core problem first—here are the key checks and fixes:

  1. Missing Firebase SDK Scripts in HTML
    Your HTML doesn't load the required Firebase JavaScript SDK files, which means your app.js can't access the firebase object at all. Add these scripts right before your app.js script tag in the HTML body:

    <!-- Firebase Core SDK -->
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
    <!-- Firebase Realtime Database SDK -->
    <script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js"></script>
    

    You can grab the latest SDK version from your Firebase Console's "Add web app" wizard if needed.

  2. Incomplete Firebase Configuration
    Make sure your firebaseConfig in app.js is filled with your actual project credentials. It should look like this (replace placeholders with your project's values):

    const firebaseConfig = {
      apiKey: "YOUR_ACTUAL_API_KEY",
      authDomain: "smarthome-e154e.firebaseapp.com",
      databaseURL: "https://smarthome-e154e-default-rtdb.firebaseio.com",
      projectId: "smarthome-e154e",
      storageBucket: "smarthome-e154e.appspot.com",
      messagingSenderId: "YOUR_SENDER_ID",
      appId: "YOUR_APP_ID"
    };
    

    You can copy this exact snippet from Firebase Console → Project Settings → "Add app" → Web app.

  3. Fix the Broken Line in led1off
    Your led1off function has a malformed document.getElementById() call (no ID passed), which throws an error and stops the function from executing. Remove that line or correct it:

    function led1off(){
      // Remove this broken line, or add a valid ID like document.getElementById('Acak')
      database.ref('Lampu/Led0').set("0");
    }
    
  4. Relax Database Rules (for testing)
    By default, Firebase blocks unauthenticated writes to the database. If you haven't set up user auth yet, temporarily update your rules to allow public access (lock this down before production):
    Go to Firebase Console → Realtime Database → Rules, then set:

    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    

    Click "Publish" to apply changes. For production, implement proper authentication and restrict rules to authorized users.


Issue 2: Error running firebase serve --debug --only function

This error occurs because your project doesn't have Firebase Cloud Functions initialized. Here's how to fix it:

  • If you don't need Cloud Functions: Drop the --only function flag. Just run firebase serve—it will serve your static HTML/CSS/JS files (make sure you initialized Hosting during firebase init).
  • If you do need Cloud Functions: Initialize them first:
    1. Run firebase init functions in your project directory.
    2. Follow the prompts to set up your Functions environment (choose JavaScript/TypeScript, etc.).
    3. After initialization, firebase serve --only functions will work as expected.

Once you apply these fixes, test your buttons again—they should now update the Lampu/Led0 value in Firebase Realtime Database. Let me know if you hit any other snags!

内容的提问来源于stack exchange,提问作者renaldyks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:37:38