douxianliu6756 2017-12-19 09:29
浏览 97
已采纳

无法使用golang从数据存储区中基于结构值获取记录

I am writing a service for search functionality. when i am passing values in the body to get the specific record i am unable to get it based on the struct value. ex:phone number or username

Edit1:

type PatientData struct {
    ID            int64    datastore:"-"
    FirstName     string   json:"firstname,omitempty"
    LastName      string   json:"lastname,omitempty"
    UserName      string   json:"username,omitempty"
    Phone         string   json:"phone,omitempty"     
}

I want to get the full struct values based on Username or Phone. Below is my code:

func searchValue (res http.ResponseWriter, req *http.Request){

    log.Printf("%#v Getting values url - x ", "called search")

    patient := &PatientData{}
    if err := json.NewDecoder(req.Body).Decode(patient); err != nil {
        panic(err)
    }
    ctx := appengine.NewContext(req)
    ctx, err := appengine.Namespace(ctx, NAMESPACENAME)
    if err != nil {
        panic(err)
    }

    m := patient.Phone
    i, err := strconv.ParseInt(m, 10, 64)
    log.Printf("%#v Getting values m ", m)
    log.Printf("%#v Getting values url -yyy ",i)

    key := datastore.NewKey(ctx, "Patient","" , i, nil)
    log.Printf("%#v Getting values url - key ", key)
    err = datastore.Get(ctx, key, patient)
    if err := json.NewEncoder(res).Encode(patient); err != nil {
        panic(err)
    }

}

As i am passing PHONE in my Newkey i am unable to generate the values based on PHONE

I don't want to use Newkey in put functionality to generate a keyname and based on that KEYNAME i dont want to get Values.

  • 写回答

1条回答 默认 最新

  • dqjjw04440 2017-12-19 12:05
    关注

    datastore.Get() can only be used to get an entity from the datastore by its key, so its key must be known / present.

    This is obviously not what you're trying to do. You are trying to fetch entities by properties which are not the key. To do that, you need to run a query.

    Datastore queries are represented by the datastore.Query type. You need to create a query and set filters on it. In your case, you want to filter by the username and/or phone properties.

    This is how it could look like. Fetch patient entities filtered by phone:

    q :=  datastore.NewQuery("Patient").Filter("phone =", patient.Phone)
    
    var patients []*Patient
    keys, err := q.GetAll(ctx, &patients)
    if err != nil {
        // Handle error
        return
    }
    
    // patients contains patients with the given phone number
    

    An example fetching patients filtered by phone number AND user name:

    q :=  datastore.NewQuery("Patient").
        Filter("phone =", patient.Phone).
        Filter("username =", patient.UserName)
    
    var patients []*Patient
    keys, err := q.GetAll(ctx, &patients)
    if err != nil {
        // Handle error
        return
    }
    
    // patients contains patients with the given phone number and username
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)