react-native - 无法使用 react native 将数据推送到实时数据库

我正在使用此代码通过 NodeJS 推送到实时数据库,这工作正常

import admin from "firebase-admin";


admin.initializeApp({
  credential: admin.credential.cert(process.env.GOOGLE_APPLICATION_CREDENTIALS),
  databaseURL: 'https://xxxxx1.firebasedatabase.app/'
});


var db = admin.database();
var ref = db.ref("queue");

ref.child('tasks').push({"id": "Some ID"}).then(function(){ process.exit();});

我想在 react-native 应用程序中推送相同的数据。我尝试了以下代码,但没有任何反应。

import database from '@react-native-firebase/database';

   const newReference = database().ref('queue')
    newReference.child('tasks').push({
      id: "Some ID"
    })

    const reference = database().ref('/queue/tasks').push();
    reference
      .set({
        id: "Some ID",
      })
      .then(() => console.log('Data updated.'));
      .catch(function(error) {
  //error callback
  console.log('Something went wrong ', error)

  })

规则编辑,以便每个人都可以推入数据库

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

回答1

我发现了问题。我错过了文档中的这一部分

注意:要获取对“us-central1”默认数据库以外的数据库的引用,您必须传递数据库 URL。您可以在 Firebase 控制台的实时数据库部分找到您的实时数据库 URL。

// firebase should be imported and not database
import { firebase } from '@react-native-firebase/database';

const reference = firebase
  .app()
  .database('https://<databaseName>.<region>.firebasedatabase.app/')
  .ref('/users/123');

相似文章

随机推荐

最新文章