博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD
阅读量:4353 次
发布时间:2019-06-07

本文共 2164 字,大约阅读时间需要 7 分钟。

Install:

npm i --save firebase // v3.2.1

 

Config Firebase:

First we need to require Firebase:

import firebase from 'firebase';

Then in the component constructor, we need to init Firebase:

constructor(props){        super(props);        // Initialize Firebase        var config = {            apiKey: "
", authDomain: "
", databaseURL: "
", storageBucket: "
", }; firebase.initializeApp(config); }

The config you can easily get from the your Firebase App page.

 

Create new node: set()

writeUserData(userId, name, email, imageUrl) {        firebase.database().ref('users/' + userId).set({            username: name,            email: email,            profile_picture : imageUrl        });    }// calling    this.writeUserData(1, "Zhentian Wan", 'answer881215@gmail.com', null);

Then one node will be created in the "users" node, uid is "1".

 

Create new Child node: push()

appendOneMoreUser(name, email, imageUrl){        let ref = firebase.database().ref().child('users').push({            username: name,            email: email        });        this.updateMe("Yoona", "yoona.only@gmail.com", ref.key);    }// callingthis.appendOneMoreUser("Yuri", 'yuri.only@gmail.com', null);

So under "users" node, we want to append one new node, the uid will be generated randomly.

The "ref" object contains "key" prop which ref to the uid in the database.

 

Update one node: update()

updateMe(name, email, key){        const update = {            username: name,            email        };        let ref = firebase.database().ref('users/' + key).update(update)            .then((res)=>{                console.log("Data has been updated ");            });    }// calling:const ref = firsebase.database().ref().child('users').push(user);this.updateMe("Yoona", "yoona.only@gmail.com", ref.key);

update() and set() methods both return Promise. So you can chaining .then() onto it.

 

Delete node: remove()

deleteMe(){        firebase.database().ref('users/1').remove();    }

Delete the uid = 1 user.

 

转载于:https://www.cnblogs.com/Answer1215/p/5735188.html

你可能感兴趣的文章
Java JDBC
查看>>
走势终完美 --执子之手
查看>>
补全左括号
查看>>
javascript中关于坐标 大小 的描述
查看>>
8086CPU各寄存器的用途
查看>>
AngularJs中,如何在render完成之后,执行Js脚本
查看>>
Nginx 防盗链
查看>>
如何讓Android系統顯示CJK擴展區漢字
查看>>
Android 下拉选择绑定Value和Text值
查看>>
HTML+CSS小结
查看>>
Android防止按钮连续点击
查看>>
ElasticSearch Mapping中的字段类型
查看>>
数据库中主键和外键的设计原则
查看>>
怎样理解阻塞非阻塞与同步异步的区别?
查看>>
Xcode 警告信息处理:Format string is not a string literal (potentially insecure)
查看>>
关于jQuery表单校验的应用
查看>>
matplotlib----初探------5直方图
查看>>
jquery之ajax
查看>>
Pro Git(中文版)
查看>>
解决phpmyadmin-1800秒超时链接失效问题
查看>>