使用数据初始化<StreamApp>

时间:2019-07-17 14:28:30

标签: getstream-io

我正在使用本机代码与本机代码进行交互。使用“ react-native-activity-feed”库,我试图在我的应用程序上启动并运行Stream Feed。

我已经使用api密钥,应用ID和应用密钥设置了我的流帐户。使用firebase函数,我将在创建帐户时为用户创建令牌服务器端,然后将其保存到数据库中。

const client = stream.connect(
    functions.config().stream.key_id,
    functions.config().stream.secret,
);
const userToken = client.createUserToken(user.uid);
admin.database().ref('users/' + user.uid ).set({
    token: userToken
})

客户端,我为当前用户检索该令牌,然后将其(以及应用程序ID和应用程序密钥)传递给对象。我期望得到一个没有错误的空提要,但是我从Stream中得到了“数据未定义”错误。

要开始使用StreamApp对象,我还需要创建客户端或服务器端什么?

import {StreamApp} from 'react-native-activity-feed



render() {
  const { currentUser } = this.state
  const {streamToken} = this.state

  let apiKey = Config.STREAM_API_KEY;
  let appId = Config.STREAM_APP_ID;
  userId = firebase.auth().currentUser.uid;
  let token;
  tokenRef = firebase.database().ref(`users/${userId}`);  
  tokenRef.on('value', (snapshot) => {
    token = snapshot.val().token;
  })

return (
      <StreamApp
      apiKey={apiKey}
      appId={appId}
      token={token}
     >
      </StreamApp>
    )
  }
}

错误:

TypeError:undefined不是一个对象(正在评估“ client.currentUser.data”)

此错误位于:

在StreamApp中

(...) ...

1 个答案:

答案 0 :(得分:0)

最终导致服务器在返回流令牌之前调用了render函数。

要解决,我给页面时间从服务器加载流令牌:

render() {
    if(this.state.loading) {
      return (<View style={[styles.container, styles.horizontal]}><ActivityIndicator size="large" color="#0000ff" /></View>);
    }
    return (
      <StreamApp
      apiKey={this.state.apiKey}
      appId={this.state.apiId}
      token={this.state.streamToken}
     >
    </StreamApp>
    )
  }
}