Javascript 如何将数组中的JSON数据保存到Firestore
我正在尝试将JSON数据保存到Firestore。 数据如下:Javascript 如何将数组中的JSON数据保存到Firestore,javascript,google-cloud-firestore,Javascript,Google Cloud Firestore,我正在尝试将JSON数据保存到Firestore。 数据如下: [{"back":"democracy","delayed_till":{"seconds":1574944200,"nanoseconds":0},"examples":[{"answer":"en Espana existe democracia desde 1975","example":"There is democracy in Spain since 1975"},{"answer":"Debemos luchar po
[{"back":"democracy","delayed_till":{"seconds":1574944200,"nanoseconds":0},"examples":[{"answer":"en Espana existe democracia desde 1975","example":"There is democracy in Spain since 1975"},{"answer":"Debemos luchar por nuestra democracia","example":"We must fight for our democracy"}],"front":"la democracia"},{"back":"habit, custom","delayed_till":{"seconds":1575148500,"nanoseconds":0},"front":"la costumbre"},{"back":"krokodil","delayed_till":{"seconds":1577831400,"nanoseconds":0},"front":"roos"},{"back":"krokodil","front":"blabla"},{"back":"bjkla","front":"blabla"}]
由于JSON数据存储在一个数组中,因此在以下情况下将其保存到Firestore不起作用
db.collection('cardsb').add(object)
使用以下命令仅保存JSON数据中的第一个对象
db.collection('cardsb').add(object[0])
将保存的零件如下所示:
{"back":"democracy","delayed_till":{"seconds":1574944200,"nanoseconds":0},"examples":[{"answer":"en Espana existe democracia desde 1975","example":"There is democracy in Spain since 1975"},{"answer":"Debemos luchar por nuestra democracia","example":"We must fight for our democracy"}],"front":"la democracia"}
如何将整个数据保存在一个文档中
编辑
以下是创建JSON数据的方式:
cons () {
const nbrGroups = this.cards.length
for (let i = 0; i < nbrGroups; i++) {
var object = []
const nbrItems = this.cards[i].group.length
for (let item = 0; item < nbrItems; item++) {
var inputs = document.querySelectorAll('#' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inFront, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inBack, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inAnswer, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inExample')
const examples = []
if (inputs.length !== 2) {
for (let i = 2; i < inputs.length; i++) {
examples.push({ answer: inputs[i + 1].value, example: inputs[i].value })
i++
}
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
examples,
front: inputs[0].value
})
} else {
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
front: inputs[0].value
})
}
}
console.log(JSON.stringify(object))
console.log(JSON.stringify(object) === JSON.stringify(this.cards[i].group))
console.log(JSON.stringify(this.cards[i].group))
console.log(JSON.stringify(this.cards[i].id))
db.collection('cardsb').add(object[0])
}
cons(){
常量组=this.cards.length
for(设i=0;i<0;i++){
变量对象=[]
const nbr items=this.cards[i].group.length
对于(设项=0;项<0;项++){
var inputs=document.queryselectoral(“#”+this.cards[i].组名.replace(/\s/g),)+item+'A#inFront,#')+this.cards[i].组名.replace(/\s/g,)+item+'A#inBack,#'+this.cards[i].组名
常量示例=[]
if(inputs.length!==2){
for(设i=2;i
以下是它在FB中的存储方式:
这就是实际存储的内容
据我所知,您只是过早地将数据写入Firestore。您只应在处理完所有组后才进行写入,此时应一次写入所有组 比如:
const nbrGroups = this.cards.length
var object = []; // Move this before the for loop
for (let i = 0; i < nbrGroups; i++) {
const nbrItems = this.cards[i].group.length
for (let item = 0; item < nbrItems; item++) {
var inputs = document.querySelectorAll('#' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inFront, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inBack, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inAnswer, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inExample')
const examples = []
if (inputs.length !== 2) {
for (let i = 2; i < inputs.length; i++) {
examples.push({ answer: inputs[i + 1].value, example: inputs[i].value })
i++
}
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
examples,
front: inputs[0].value
})
} else {
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
front: inputs[0].value
})
}
}
}
db.collection('cardsb').add(object[0]); // move this after the for loop
const nbrGroups=this.cards.length
var object=[];//将其移动到for循环之前
for(设i=0;i<0;i++){
const nbr items=this.cards[i].group.length
对于(设项=0;项<0;项++){
var inputs=document.queryselectoral(“#”+this.cards[i].组名.replace(/\s/g),)+item+'A#inFront,#')+this.cards[i].组名.replace(/\s/g,)+item+'A#inBack,#'+this.cards[i].组名
常量示例=[]
if(inputs.length!==2){
for(设i=2;i
据我所知,您只是过早地将数据写入Firestore。您只应在处理完所有组后才进行写入,此时应一次写入所有组
比如:
const nbrGroups = this.cards.length
var object = []; // Move this before the for loop
for (let i = 0; i < nbrGroups; i++) {
const nbrItems = this.cards[i].group.length
for (let item = 0; item < nbrItems; item++) {
var inputs = document.querySelectorAll('#' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inFront, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inBack, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inAnswer, #' + this.cards[i].group_name.replace(/\s/g, '') + item + 'A #inExample')
const examples = []
if (inputs.length !== 2) {
for (let i = 2; i < inputs.length; i++) {
examples.push({ answer: inputs[i + 1].value, example: inputs[i].value })
i++
}
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
examples,
front: inputs[0].value
})
} else {
object.push({
back: inputs[1].value,
delayed_till: this.cards[i].group[item].delayed_till,
front: inputs[0].value
})
}
}
}
db.collection('cardsb').add(object[0]); // move this after the for loop
const nbrGroups=this.cards.length
var object=[];//将其移动到for循环之前
for(设i=0;i<0;i++){
const nbr items=this.cards[i].group.length
对于(设项=0;项<0;项++){
var inputs=document.queryselectoral(“#”+this.cards[i].组名.replace(/\s/g),)+item+'A#inFront,#')+this.cards[i].组名.replace(/\s/g,)+item+'A#inBack,#'+this.cards[i].组名
常量示例=[]
if(inputs.length!==2){
for(设i=2;i
请更新您的问题,以显示对象的填充方式,或者包括执行控制台.log(JSON.stringify(object))
的代码,并在您的问题中包括记录的输出。如果您显示(可能带有Firebase控制台的屏幕截图)Firestore中存储的内容,以及(可能是手动创建的文档)您希望存储的内容。Firestore文档的本机数据类型是对象(映射)。它不存储数组,只存储文档字段。@Frank我更新了我的问题。我希望它有意义:)注意:这里没有JSON。您将JavaScript对象/数组传递给Firestore,而不是JSON。如果您执行。添加({group:object})
请更新您的问题,以显示对象
是如何填充的,或者包含执行控制台.log(JSON.stringify(object))的代码
并将记录的输出包含在您的问题中。如果您显示(可能带有