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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀