带有create-react-app的Firebase 3.5.2不起作用(Firebase 3.5.2 with create-react-app doesn't works)
我按照这个官方Firebase Youtube指南一步一步地进行了操作。 我想将Firebase用于由create-react-app创建的React应用,就像官方Firebase频道中的示例一样。 我用一个小表单的成瘾生成了以下代码:
index.js
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import './index.css'; import * as firebase from 'firebase' const config = { apiKey: "SUPERSECRET", authDomain: "SUPERSECRET.firebaseapp.com", databaseURL: "https://SUPERSECRET.firebaseio.com", storageBucket: "", messagingSenderId: "SUPERSECRET" }; firebase.initializeApp(config); ReactDOM.render( <App />, document.getElementById('root') );
app.js
import React, { Component } from 'react'; import * as firebase from 'firebase'; export default class PageOne extends Component{ constructor(){ super(); this.state = { name: "example" } } submitValue(e){ e.preventDefault(); console.log("pressed"); const ref = firebase.database().ref().child("names"); ref.set({ name: e.target.value }) } componentDidMount(){ const ref = firebase.database().ref().child("names"); ref.on('value', snapshot => { this.setState({ name: snapshot.val() }); }) } render(){ return( <div> <h1>{this.state.name}</h1> <form onSubmit={this.submitValue.bind(this)} className="form"> <input type="text" id="nameField"/> <input type="submit" /> </form> </div> ) } }
没有控制台日志错误出现,没有来自Firebase的响应。 怎么了? 谢谢!!
更新1:
我错过了将关键字添加到snapshot.value:
componentDidMount(){ const ref = firebase.database().ref().child("names"); ref.on('value', snapshot => { this.setState({ name: snapshot.val().name }); }) }
现在我可以从数据库中读取数据,但在提交表单时我无法写入数值。
I followed step by step this official Firebase Youtube guide. I want to use Firebase into a React app created by create-react-app, like the example in the official Firebase channel. I generated the following code with the addiction of a small form:
index.js
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import './index.css'; import * as firebase from 'firebase' const config = { apiKey: "SUPERSECRET", authDomain: "SUPERSECRET.firebaseapp.com", databaseURL: "https://SUPERSECRET.firebaseio.com", storageBucket: "", messagingSenderId: "SUPERSECRET" }; firebase.initializeApp(config); ReactDOM.render( <App />, document.getElementById('root') );
app.js
import React, { Component } from 'react'; import * as firebase from 'firebase'; export default class PageOne extends Component{ constructor(){ super(); this.state = { name: "example" } } submitValue(e){ e.preventDefault(); console.log("pressed"); const ref = firebase.database().ref().child("names"); ref.set({ name: e.target.value }) } componentDidMount(){ const ref = firebase.database().ref().child("names"); ref.on('value', snapshot => { this.setState({ name: snapshot.val() }); }) } render(){ return( <div> <h1>{this.state.name}</h1> <form onSubmit={this.submitValue.bind(this)} className="form"> <input type="text" id="nameField"/> <input type="submit" /> </form> </div> ) } }
No console log error appears, no response from Firebase. What's wrong? Thanks!!
update 1:
I missed to add the key to the snapshot.value:
componentDidMount(){ const ref = firebase.database().ref().child("names"); ref.on('value', snapshot => { this.setState({ name: snapshot.val().name }); }) }
Now I can read from DB, but I can't write values when I submit form.
原文:https://stackoverflow.com/questions/40251130
最满意答案
这提供了一些改进(> 2x):
split(Reduce(`+`, lapply(m2, unlist)), rep(seq_along(m2[[1]]), lengths(m2[[1]])))
由于您的数据基本上是矩形的,因此您将其存储为以下形状:
library(data.table) d = rbindlist(lapply(m2, function(x) transpose(as.data.table(x))), id = T )[, id.in := 1:.N, by = .id] # .id V1 V2 V55 id.in #1: 1 0.4605065 0.09744975 ... 0.8620728 1 #2: 1 0.6666742 0.10435471 ... 0.3991940 2 #3: 2 0.4605065 0.09744975 ... 0.8620728 1 #4: 2 0.6666742 0.10435471 ... 0.3991940 2 #5: 3 0.4605065 0.09744975 ... 0.8620728 1 #6: 3 0.6666742 0.10435471 ... 0.3991940 2
通过执行以下操作,您可以更快地进行聚合:
d[, lapply(.SD, sum), by = id.in]
但如果列表是您的起点,转换将占用大部分时间。
This gives some improvement (>2x):
split(Reduce(`+`, lapply(m2, unlist)), rep(seq_along(m2[[1]]), lengths(m2[[1]])))
Since your data is essentially rectangular, had you stored it in this shape:
library(data.table) d = rbindlist(lapply(m2, function(x) transpose(as.data.table(x))), id = T )[, id.in := 1:.N, by = .id] # .id V1 V2 V55 id.in #1: 1 0.4605065 0.09744975 ... 0.8620728 1 #2: 1 0.6666742 0.10435471 ... 0.3991940 2 #3: 2 0.4605065 0.09744975 ... 0.8620728 1 #4: 2 0.6666742 0.10435471 ... 0.3991940 2 #5: 3 0.4605065 0.09744975 ... 0.8620728 1 #6: 3 0.6666742 0.10435471 ... 0.3991940 2
You could do the aggregation even faster by doing:
d[, lapply(.SD, sum), by = id.in]
But if the list is your starting point, the conversion would take up the majority of the time.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
zip功能在这里很有用,与列表理解一起使用。 [x + y for x, y in zip(first, second)] 如果您有列表列表(而不仅仅是两个列表): lists_of_lists = [[1, 2, 3], [4, 5, 6]] [sum(x) for x in zip(*lists_of_lists)] # -> [5, 7, 9] The zip function is useful here, used with a list comprehension. [x + y for x ...
-
不要忘记准确描述你想要做的事情。 我假设你的意思是把所有的数值总和为一个数值,而不是例如。 [[1,2],[3,4]] - > [3,7]。 这是简单的递归; 如果您跳过测试,请使用五行代码: def sums(it): """ >>> sums(1) 1 >>> sums([1,2,3]) 6 >>> sums([1,2,3,[4,5]]) 15 >>> sums(['a','b']) Traceback (most recent c ...
-
如何在特定子列表的字段上列出多个列表清单?(How do I sum multiple lists-of-lists on certain sub-list's fields?)[2022-07-17]
为了说明为什么使用正确的数据结构使事情变得更容易... 假设a , b和c实际上是dict s,而你的数字实际上是int s而不是str s。 毕竟,一个dict的全部要点是按名称来查找,而整数的整点是能够进行算术运算。 所以: a = {'jane': [1, 120], 'bob': [3, 35], 'joe': [5, 70]} b = {'bob': [1, 12], 'jane': [2, 240], 'joe': [1, 100]} c = {'joe': [2, 30], 'jane': [ ... -
Future.sequence将Traversable[Future[T]]转换为Future[Traversable[T]] (如果原始列表中的任何未来失败,这个未来将失败)。 之后,你可以简单地调用这个未来的内容sum : Future.sequence(x).map(_.sum) Future.sequence transforms a Traversable[Future[T]] into a Future[Traversable[T]] (this future will be failed, ...
-
询问用户并打印一份清单(Ask user and print a list)[2022-08-14]
您需要将列表映射到字符串,这是用户可以输入的内容。 所以使用一个字典: lists_dict = { 'a': [1,2,3,4,5] 'b': [6,7,8,9,10] } key = input("Write a list name") print lists_dict[key] 编辑: 你的字典应该如下所示: poland = { "poznan": {"name": 86470, "lst": [1711505, 163780, 932461, 1164703]}, ... -
对于第一个,您可以使用map()和sum() : >>> a = [[2,8,324,564,35,6],[4,6,23,65,213,5],[432,54,12,65,32,1]] >>> >>> map(sum, a) [939, 316, 596] >>> 第二个使用map , sum和zip : >>> map(sum, zip(*a)) [438, 68, 359, 694, 280, 12] 阅读有关内置函数的更多信息https://docs.python.org/3/library/f ...
-
总结列表清单?(Sum a list of lists?)[2021-09-09]
编辑:现在它更清楚你实际需要什么:)你有一个矩阵表示列表的列表,并希望提取列的总和。 要做到这一点,只需转置矩阵并计算行总和: Prelude> import Data.List Prelude Data.List> map sum . transpose $ [ [1,1,1], [2,2,2], [3,3,3] ] [6,6,6] EDIT: Now it's more clear what you actually need :) You have a matrix represented as a ... -
快速总结一份清单?(Quickly sum a big list of lists?)[2023-08-21]
这提供了一些改进(> 2x): split(Reduce(`+`, lapply(m2, unlist)), rep(seq_along(m2[[1]]), lengths(m2[[1]]))) 由于您的数据基本上是矩形的,因此您将其存储为以下形状: library(data.table) d = rbindlist(lapply(m2, function(x) transpose(as.data.table(x))), id = T )[, id.in := 1:.N, by ...