douxianliu6756 2017-12-19 01: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 04: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 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥20 DNS服务器所在的国家不同与你的IP地址所在国家
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
  • ¥15 结果有了,想问一下这个具体怎么输入
  • ¥15 怎么修改鸿蒙app的UI及功能设计
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部