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

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

在 Go api 中接收不一致的數(shù)據(jù)

在 Go api 中接收不一致的數(shù)據(jù)

Go
寶慕林4294392 2022-09-26 20:16:06
我正在學(xué)習(xí)Go語(yǔ)言,并使用go + 谷歌火庫(kù)作為數(shù)據(jù)庫(kù)測(cè)試了谷歌云功能。當(dāng)我測(cè)試響應(yīng)時(shí),我得到了不一致的響應(yīng)。我使用 json 編組器將 Firebase 數(shù)據(jù)轉(zhuǎn)換為 Json 對(duì)象以從 API 返回,此 API 托管在 Google 云函數(shù)中。// Package p contains an HTTP Cloud Function.package pimport (    "context"    "fmt"    "log"    "net/http"    s "strings"    "encoding/json"    "cloud.google.com/go/firestore"    "google.golang.org/api/iterator")func HelloWorld(w http.ResponseWriter, r *http.Request) {    path := s.Replace(r.URL.Path, "/", "", -1)    ctx := context.Background()    client := createClient(ctx)    iter := client.Collection("profile").Where("publicUrl", "==", path).Documents(ctx)    for {        doc, err := iter.Next()        if err == iterator.Done {            break        }        var publicDTO PublicDTO        var Profile Profile        doc.DataTo(&Profile)        publicDTO.Profile = Profile        b, err := json.Marshal(publicDTO)        if err != nil {            fmt.Println(err)            return        }        w.Header().Set("Content-Type", "application/json")        w.Write(b)    }}func createClient(ctx context.Context) *firestore.Client {    projectID := "projectId"    client, err := firestore.NewClient(ctx, projectID)    if err != nil {        log.Fatalf("Failed to create client: %v", err)    }    return client}type PublicDTO struct {    Profile       Profile     `json:"profile"`}type Profile struct {    Id           string            `json:"id"`    FirstName    string            `json:"firstName"`    LastName     string            `json:"lastName"`    FullName     string            `json:"fullName"`    Email        string            `json:"email"`    ImageUrl     string            `json:"imageUrl"`    CoverPic     string            `json:"coverPic"`    Experience   int               `json:"experience"`    PhoneNumber  string            `json:"phoneNumber"`}但是每次我得到的響應(yīng)不一致時(shí),一些值都丟失了。
查看完整描述

1 回答

?
守著星空守著你

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

我在元帥和解帥之后得到的解決方案,它按預(yù)期工作。


package p


import (

    "context"

    "encoding/json"

    "fmt"

    "log"

    "net/http"

    s "strings"

    "time"


    "cloud.google.com/go/firestore"

    "google.golang.org/api/iterator"

)


var ctx context.Context

var client *firestore.Client


func PublicApi(w http.ResponseWriter, r *http.Request) {

    path := s.Replace(r.URL.Path, "/", "", -1)

    w.Header().Set("Content-Type", "application/json")

    w.Header().Set("Access-Control-Allow-Origin", "*")

    w.Header().Set("Access-Control-Allow-Headers", "*")


    newFsConfigBytes, _ := json.Marshal(getPublicDTO(path))

    w.Write(newFsConfigBytes)

}


func getPublicDTO(path string) (publicDTO publicDTO) {

    ctx = context.Background()

    client = createClient()

    profile, id := getProfiles(path)

    publicDTO.Profile = profile

    publicDTO.MilestoneDTOS = getMilestone(id)

    return

}


func getProfiles(link string) (profile, string) {

    var retVal profile

    var id string

    iter := client.Collection("profile").Where("publicUrl", "==", link).Documents(ctx)

    for {

        doc, err := iter.Next()

        if err == iterator.Done {

            break

        }

        id = doc.Ref.ID

        b, _ := json.Marshal(doc.Data())

        json.Unmarshal(b, &retVal)

    }

    return retVal, id

}


func getMilestone(id string) []milestoneDTOS {

    var retVal []milestoneDTOS

    iter := client.Collection("milestone").Where("userId", "==", id).Documents(ctx)


    for {

        var milestoneDTO milestoneDTOS

        doc, err := iter.Next()

        if err == iterator.Done {

            break

        }

        b, _ := json.Marshal(doc.Data())

        err = json.Unmarshal(b, &milestoneDTO)

        if err != nil {

            fmt.Println(err)

        }

        retVal = append(retVal, milestoneDTO)

    }

    return retVal

}


func createClient() *firestore.Client {

    projectID := "live-hiveboard"


    client, err := firestore.NewClient(ctx, projectID)

    if err != nil {

        log.Fatalf("Failed to create client: %v", err)

    }

    return client

}


type profile struct {

    Address          string    `json:"address"`

    City             string    `json:"city"`

    Country          string    `json:"country"`

    CoverPic         string    `json:"coverPic"`

    CreatedBy        string    `json:"createdBy"`

    CreatedDate      int       `json:"createdDate"`

    Description      string    `json:"description"`

    Dob              int64     `json:"dob"`

    Email            string    `json:"email"`

    Enabled          bool      `json:"enabled"`

    Experience       int       `json:"experience"`

    FirstName        string    `json:"firstName"`

    FullName         string    `json:"fullName"`

    FullNameNoSpace  string    `json:"fullNameNoSpace"`

    ImageURL         string    `json:"imageUrl"`

    Interests        []string  `json:"interests"`

    IsEnabled        bool      `json:"isEnabled"`

    Language         string    `json:"language"`

    LastModifiedDate int       `json:"lastModifiedDate"`

    LastName         string    `json:"lastName"`

    LatLng           string    `json:"latLng"`

    MemberFrom       time.Time `json:"memberFrom"`

    ObjectID         string    `json:"objectID"`

    Organization     string    `json:"organization"`

    PhoneNumber      string    `json:"phoneNumber"`

    PlanID           string    `json:"planId"`

    PublicURL        string    `json:"publicUrl"`

    Reputation       int       `json:"reputation"`

    Setup            int       `json:"setup"`

    Social           string    `json:"social"`

    State            string    `json:"state"`

    Status           string    `json:"status"`

    Title            string    `json:"title"`

    Website          string    `json:"website"`

}


type milestoneDTOS struct {

    Category          string    `json:"category"`

    CreatedBy         string    `json:"createdBy"`

    CreatedDate       int       `json:"createdDate"`

    Description       string    `json:"description"`

    Enabled           bool      `json:"enabled"`

    EndDate           time.Time `json:"endDate"`

    IsCurrentPosition bool      `json:"isCurrentPosition"`

    IsEnabled         bool      `json:"isEnabled"`

    LastModifiedBy    time.Time `json:"lastModifiedBy"`

    LastModifiedDate  int       `json:"lastModifiedDate"`

    ObjectID          string    `json:"objectID"`

    Organization      string    `json:"organization"`

    PictureURL        string    `json:"pictureURL"`

    Profile           string    `json:"profile"`

    Score             float64   `json:"score"`

    StartDate         time.Time `json:"startDate"`

    Tags              []string  `json:"tags"`

    Title             string    `json:"title"`

    URL               string    `json:"url"`

    UserID            string    `json:"userId"`

}


type publicDTO struct {

    Profile       profile         `json:"profile"`

    MilestoneDTOS []milestoneDTOS `json:"milestoneDTOS"`

}


查看完整回答
反對(duì) 回復(fù) 2022-09-26
  • 1 回答
  • 0 關(guān)注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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