Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
firebase function with realtime database error

I am new to firebase function and trying to use firebase function with Realtime database (Emulator suite).But when i try to set the value in firebase using the firebase function,it gives an error and doesn't set the value in database.

Error:

17:33:14
I
function[us-central1-textToLength]
[2021-11-05T12:03:14.194Z]  @firebase/database: FIREBASE WARNING: wss:// URL used, but browser isn't known to support websockets.  Trying anyway. 
17:34:18
I
function[us-central1-textToLength]
[2021-11-05T12:04:18.762Z]  @firebase/database: FIREBASE WARNING: wss:// URL used, but browser isn't known to support websockets.  Trying anyway. 
17:35:06
I
function[us-central1-textToLength]
[2021-11-05T12:05:06.473Z]  @firebase/database: FIREBASE WARNING: wss:// URL used, but browser isn't known to support websockets.  Trying anyway. 
17:35:54
I
function[us-central1-textToLength]
[2021-11-05T12:05:54.409Z]  @firebase/database: FIREBASE WARNING: wss:// URL used, but browser isn't known to support websockets.  Trying anyway. 

firebase function code :

const functions = require('firebase-functions');
var admin = require("firebase-admin");
admin.initializeApp(); 

var database = admin.database();

exports.helloWorld = functions.https.onRequest((request, response) => {
    response.send("Hello from Firebase!");
});

exports.textToLength = functions.https.onRequest((request, response) => {
    var tex = request.query.text;
    var textLength = tex.length;
    console.log(textLength);
    database.ref().child('test').set("op");
    response.send(String(textLength));
});

dependencies :

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1",
    "@firebase/database-compat": "0.1.2" 
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

npm installed packages

+-- @firebase/app-compat@0.1.7
| +-- @firebase/app@0.7.6
| | +-- @firebase/component@0.5.8
| | | +-- @firebase/util@1.4.1 deduped
| | | `-- tslib@2.3.1 deduped
| | +-- @firebase/logger@0.3.1
| | | `-- tslib@2.3.1 deduped
| | +-- @firebase/util@1.4.1
| | | `-- tslib@2.3.1 deduped
| | `-- tslib@2.3.1 deduped
| +-- @firebase/component@0.5.8
| | +-- @firebase/util@1.4.1 deduped
| | `-- tslib@2.3.1 deduped
| +-- @firebase/logger@0.3.1
| | `-- tslib@2.3.1 deduped
| +-- @firebase/util@1.4.1
| | `-- tslib@2.3.1 deduped
| `-- tslib@2.3.1
+-- @firebase/database-compat@0.1.2
| +-- @firebase/component@0.5.7
| | +-- @firebase/util@1.4.0 deduped
| | `-- tslib@2.3.1 deduped
| +-- @firebase/database@0.12.2
| | +-- @firebase/auth-interop-types@0.1.6
| | +-- @firebase/component@0.5.7 deduped
| | +-- @firebase/logger@0.3.0 deduped
| | +-- @firebase/util@1.4.0 deduped
| | +-- faye-websocket@0.11.4
| | | `-- websocket-driver@0.7.4
| | |   +-- http-parser-js@0.5.3
| | |   +-- safe-buffer@5.2.1 deduped
| | |   `-- websocket-extensions@0.1.4
| | `-- tslib@2.3.1 deduped
| +-- @firebase/database-types@0.9.1
| | +-- @firebase/app-types@0.7.0
| | `-- @firebase/util@1.4.0 deduped
| +-- @firebase/logger@0.3.0
| | `-- tslib@2.3.1 deduped
| +-- @firebase/util@1.4.0
| | `-- tslib@2.3.1 deduped
| `-- tslib@2.3.1 deduped
`-- firebase-admin@10.0.0
  +-- @firebase/database-compat@0.1.2 deduped
  +-- @firebase/database-types@0.7.3
  | `-- @firebase/app-types@0.6.3
  +-- @google-cloud/firestore@4.15.1
  | +-- fast-deep-equal@3.1.3
  | +-- functional-red-black-tree@1.0.1
  | +-- google-gax@2.28.0
over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

I'm unsure as to the cause of that log message, but I do see that you are returning a response from your function before it completes all of its work. In a deployed function, as soon as the function returns, all further actions should be treated as if they will never be executed as documented here. An "inactive" function might be terminated at any time, is severely throttled and any network calls you make (like setting data in the RTDB) may never be executed.

I know you are new to this, but its a good habit to get into now: don't assume the person calling your function is you. Check for problems like missing query parameters and dodgy data before you blindly action something. The Admin SDK bypasses your database's security rules and if you are not careful a malicious user can cause some damage (e.g. a user that updates /users/$theirUid/roles/admin to true).

exports.textToLength = functions.https.onRequest((request, response) => {
    const { text: queryText } = request.query;
    
    if (!queryText)
      return response.status(400).send("Missing ?text");
    if (typeof queryText !== 'string')
      return response.status(400).send("Bad value for ?text");

    const textLength = queryText.length;
    
    database.ref()
      .child('test')
      .set("op")
      .then(() => {
        console.log(`Successfully updated /test/op to ${textLength}`);
        response.send(String(textLength))
      })
      .catch((err) => {
        console.error(`Failed to update /test/op to ${textLength}: `, error);
        response.status(500).send(`Failed to update database to ${textLength}`)
      });
    ;
});

Note: When deploying HTTPS Request functions, don't forget to handle CORS.

over 4 years ago · Santiago Trujillo Relatório

0

In the meantime, if you are on the latest Admin SDK version, you can pin @firebase/database-compat to version 0.1.2 in your package.json file as a temporary fix.

"dependencies": { "@firebase/database-compat": "0.1.2" }

This works for me.

Ref: https://github.com/firebase/firebase-admin-node/issues/1487

over 4 years ago · Santiago Trujillo Relatório

0

Yes, definitely known issue currently they are working on a fix like Kasirajan suggested https://github.com/firebase/firebase-admin-node/issues/1487#issuecomment-962219551. This fix worked for us. Wanted to upvote his comment but my reputation is still low haha but I can verify that works.

over 4 years ago · Santiago Trujillo Relatório

0

In the meantime, if you are on the latest Admin SDK version, you can pin @firebase/database-compat to version 0.1.2 in your package.json file as a temporary fix.

"dependencies": { "@firebase/database-compat": "0.1.2" }

This works for me.

Ref: https://github.com/firebase/firebase-admin-node/issues/1487

I referred this example and it worked but for this to work rebuild your package-lock.json file by removing node_modules folder and package_lock.json file and running npm install --package-lock-only

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda