第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Go Firestore 從集合中獲取所有文檔

Go Firestore 從集合中獲取所有文檔

Go
SMILET 2022-06-01 11:37:54
使用 go 和 firestore 創(chuàng)建 Web 應(yīng)用程序。我遇到了一個(gè)奇怪的問題。如果我使用 NewDoc 方法保存數(shù)據(jù)    ref := client.Collection("blogs").NewDoc()    _, err := ref.Set(ctx, mapBlog)    if err != nil {        // Handle any errors in an appropriate way, such as returning them.        log.Printf("An error has occurred: %s", err)    }我有能力使用    var bs models.Blogs    iter := client.Collection("blogs").Documents(ctx)    for {        var b models.Blog        doc, err := iter.Next()        if err != nil {            fmt.Println(err)        }        if err == iterator.Done {            break        }        if err := doc.DataTo(&b); err != nil {            fmt.Println(doc.Data())            bs = append(bs, b)        }    }現(xiàn)在,當(dāng)我只想查找博客集合中的所有文檔時(shí),這非常棒。但是后來我遇到了無法從博客集合中查詢特定博客的問題。我通過查看文檔并保存這樣的帖子解決了這個(gè)問題。//p is a struct and p.ID is just a string identifier// the docs show creating a struct with an ID and then an embedded struct within. _, err := client.Collection("blogs").Doc(p.ID).Set(ctx, p)     if err != nil {        fmt.Println(err)    }但是由于我自己創(chuàng)建了 docID,因此我使用從整個(gè)集合中檢索所有文檔 if err := doc.DataTo(&b); err != nil {            fmt.Println(doc.Data())            bs = append(bs, b)            fmt.Println(b)        }不再工作?;旧衔倚枰軌?yàn)橐豁摷虞d所有博客,然后如果單擊特定博客,我需要能夠獲取 ID 并僅查找集合中的一個(gè)文檔。如果我自己設(shè)置文檔 ID,為什么 doc.DataTo 不起作用?有沒有更好的方法來通常只從集合中提取所有文檔,然后專門提取單個(gè)文檔?
查看完整描述

1 回答

?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

doc.DataTo(&b)該程序僅在返回錯(cuò)誤時(shí)將博客附加到結(jié)果中。


編寫如下代碼:


var bs models.Blogs

iter := client.Collection("blogs").Documents(ctx)

defer iter.Stop() // add this line to ensure resources cleaned up

for {

    doc, err := iter.Next()

    if err == iterator.Done {

        break

    }

    if err != nil {

        // Handle error, possibly by returning the error

        // to the caller. Break the loop or return.

        ... add code here

    }

    var b models.Blog

    if err := doc.DataTo(&b); err != nil {

        // Handle error, possibly by returning the error 

        // to the caller. Continue the loop, 

        // break the loop or return.

        ... add code here

    }

    bs = append(bs, b)

}


查看完整回答
反對(duì) 回復(fù) 2022-06-01
  • 1 回答
  • 0 關(guān)注
  • 116 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)