douzhao7014 2018-05-10 11:40
浏览 18

如果数据类型从字符串更改为布尔数据存储,则会引发错误

I am storing my struct values in google data store. Here is my struct:

type Appointment struct {
    ID                    string 
    Appointment Date      string 
    Start Time            string 
    End Time              string 
    Select Specialization string 
    Smoking Status        string
} 

I have stored some data using datastore, but later changed the data type from string to bool for the field "Smoking Status" then the data store is throwing an error:

{"error":{"message":"data store: cannot load field \"Smoking Status\" into a \"simplysthealth.Encounter\": type mismatch: string versus bool"}}

Is there any feasible solution for this?

  • 写回答

1条回答 默认 最新

  • dtmooir3395 2018-05-10 12:09
    关注
    package main
    
    // I have corrected all of your method names
    type Appointment struct {
            ID                   string
            AppointmentDate      string
            StartTime            string
            EndTime              string
            SelectSpecialization string
            SmokingStatus        string
    }
    
    type AllOldData struct {
            Data []Appointment
    }
    type FixedAppointment struct {
            ID                   string
            AppointmentDate      string
            StartTime            string
            EndTime              string
            SelectSpecialization string
            SmokingStatus        bool
    }
    
    type FixedData struct {
            Data []FixedAppointment
    }
    
    func TypeFixing() FixedData {
    
            var OldData AllOldData
            var NewData FixedData
    
            OldData = GetYourAllOldData()
    
            for i, v := range OldData.Data {
                    if v.SmokingStatus == "true" {
                            // other value exchanging
                            NewData.Data[i].SmokingStatus = true
                    } else {
                            // other value exchanging
                            NewData.Data[i].SmokingStatus = false
                    }
            }
    
            return NewData // Save the data in a new table or whatever you call it
    
    }
    
    func GetYourAllOldData() AllOldData {
            // A function that returns all old data
            return AllOldData{} // You must return return your all data
    }
    

    This is what you need to do it manually!

    评论

报告相同问题?