Guest User

Untitled

a guest
Aug 12th, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { database, storage } from '../modules/Firebase';
  3. import Post from '../components/Layouts/Post';
  4.  
  5.  
  6. export default class Feed extends Component {
  7.  
  8.     constructor(props) {
  9.  
  10.         super(props);
  11.         this.state = { posts: [] };
  12.  
  13.     }
  14.  
  15.     componentDidMount( ) {
  16.    
  17.         database.ref('posts').orderByKey().limitToLast(100).on('child_added', snapshot => {
  18.  
  19.             let content = null;
  20.             storage.ref().child(snapshot.val().content).getDownloadURL().then(url => content = url ); // setting value
  21.  
  22.             console.log('content', content); // returns initial value, in my case, null. why?
  23.  
  24.             let post = {
  25.  
  26.                 key: snapshot.key,
  27.                 author: snapshot.val().author,
  28.                 uid: snapshot.val().uid,
  29.                 content,
  30.                 description: snapshot.val().description,
  31.                 date: snapshot.val().date
  32.            
  33.             };
  34.  
  35.             this.setState({ posts: [post].concat(this.state.posts) });
  36.  
  37.         });
  38.     }
  39.  
  40.     render( ) {
  41.  
  42.         console.log('state',this.state.posts);
  43.  
  44.         return (
  45.             <div className="container">
  46.                 <div className="col-md-4 col-md-offset-4 mt-md">
  47.                     {this.state.posts.map( post => <Post key={post.key} author={post.author} uid={post.uid} content={post.content} description={post.description} date={post.date} /> )}
  48.                </div>
  49.             </div>
  50.         );
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment