Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/[email protected]
| +-- @firebase/[email protected]
| | +-- @firebase/[email protected]
| | | +-- @firebase/[email protected] deduped
| | | `-- [email protected] deduped
| | +-- @firebase/[email protected]
| | | `-- [email protected] deduped
| | +-- @firebase/[email protected]
| | | `-- [email protected] deduped
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | +-- @firebase/[email protected] deduped
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | `-- [email protected] deduped
| `-- [email protected]
+-- @firebase/[email protected]
| +-- @firebase/[email protected]
| | +-- @firebase/[email protected] deduped
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | +-- @firebase/[email protected]
| | +-- @firebase/[email protected] deduped
| | +-- @firebase/[email protected] deduped
| | +-- @firebase/[email protected] deduped
| | +-- [email protected]
| | | `-- [email protected]
| | |   +-- [email protected]
| | |   +-- [email protected] deduped
| | |   `-- [email protected]
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | +-- @firebase/[email protected]
| | `-- @firebase/[email protected] deduped
| +-- @firebase/[email protected]
| | `-- [email protected] deduped
| +-- @firebase/[email protected]
| | `-- [email protected] deduped
| `-- [email protected] deduped
`-- [email protected]
  +-- @firebase/[email protected] deduped
  +-- @firebase/[email protected]
  | `-- @firebase/[email protected]
  +-- @google-cloud/[email protected]
  | +-- [email protected]
  | +-- [email protected]
  | +-- [email protected]
like image 293
Ayush Yadav Avatar asked Nov 05 '21 12:11

Ayush Yadav


People also ask

Can I use Realtime Database and firestore at the same time?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.

What are the Realtime Database limits in Firebase?

256 MB from the REST API; 16 MB from the SDKs. The total data in each write operation should be less than 256 MB.

How do I check my Firebase Realtime Database?

The Firebase Realtime Database can be accessed directly from a mobile device or web browser; there's no need for an application server. Security and data validation are available through the Firebase Realtime Database Security Rules, expression-based rules that are executed when data is read or written.


3 Answers

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

like image 170
Kasirajan M Avatar answered Oct 26 '22 04:10

Kasirajan M


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

like image 40
Ayush Yadav Avatar answered Oct 26 '22 03:10

Ayush Yadav


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.

like image 29
3a5abi Avatar answered Oct 26 '22 04:10

3a5abi