【问题标题】:Initializing <StreamApp> with Data用数据初始化 <StreamApp>
【发布时间】:2019-08-03 02:27:40
【问题描述】:

我正在使用带有本机代码的本机反应。使用“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 is not an object (evalating 'client.currentUser.data')

此错误位于:

在 StreamApp(...) ...

【问题讨论】:

  • 能否包含用于创建令牌的函数主体,并为变量附加值(包括最终令牌)
  • 我认为您需要等待 tokenRef 被检索到,然后才将其传递给 StreamApp。我的猜测是,在这种情况下 StreamApp 得到一个空令牌
  • 就是这样,谢谢!
  • 很高兴听到这个消息,您介意自己回答这个问题吗?这可以帮助未来有类似问题的读者:)
  • 如果您使用在仪表板上创建的示例或在 react native 教程(或 reactjs 教程)中使用的键,您将拥有关于它们的数据

标签: getstream-io


【解决方案1】:

在服务器返回流令牌之前调用渲染函数最终成为问题。

为了解决这个问题,我给了页面时间从服务器加载流令牌:

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>
    )
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2017-01-26
    • 2012-08-28
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多