dongliang1996 2013-12-12 20:45
浏览 25
已采纳

KeysOnly函数不返回键

I experiment a little bit with GAE, but now I have a problem. First of all I store some stuff into the datastore, with an NewIncompleteKey. So there is the issue. My Website sends timestamps (I handle them as "ID"s) to the back-end. I parse then and want to delete them from the datastore. I thought I can do this.

type Food struct{
    Id int64
    Course string
    Name string 
    Date string
    Price float64
}

...Some Code...

func deleteEntries(mealsID []string, r *http.Request) int{
    // Get context from 
    c := appengine.NewContext(r);


    for _,id := range mealsID{
        var key *datastore.Key = nil
        q := datastore.NewQuery("Meal").Ancestor(mealStoreKey(c)).Filter("Course =", "dessert").KeysOnly()
        _, err := q.GetAll(c, key)
        if err != nil{
            return 0
        }
        log.Printf("Here the keys: %T %v ", key, key)
        log.Printf("%v ", id)
        e := datastore.Delete(c, key)
        if e != nil{
            return 33       
        }       
    }

    return len(mealsID)
}

But it doesn't work, because I get an error at the datastore.Delete() function. Anyone an idea?

Edit:

Part I:

   keys, err := q.GetAll(c, nil)
   …
   err = datastore.DeleteMulti(c, keys)

Thanks to Dave.

Part II:

I passed an String as Filter vaule to the query, but it have to be an Int64 same as in the datastore. Note to my self: You have to pass also the same type of var to the query.

func deleteEntries(mealsID []string, r *http.Request) int{
 // Get context from 
 c := appengine.NewContext(r);


 for _,id := range mealsID{
     ID,_ := strconv.Atoi(id)
     q:= datastore.NewQuery("Meal").Ancestor(mealStoreKey(c)).Filter("Id =", ID ).KeysOnly()
     keys, err := q.GetAll(c, nil)
     if err != nil{
         return 0
     }
     log.Printf("ID: %v ", id)
     log.Printf("Keys: %v ", keys)
     e := datastore.DeleteMulti(c, keys)
     if e != nil{
         log.Printf("%v ", e)
             return 0       
     }      
 }

 return len(mealsID)
}
  • 写回答

1条回答 默认 最新

  • dongzhi4239 2013-12-12 22:55
    关注

    The keys are returned from GetAll. So you should instead write:

    keys, err := q.GetAll(c, nil)
    …
    err = datastore.DeleteMulti(c, keys)
    

    GetAll ignores the dst parameter for keys-only requests – datastore reference. So, in the example above, key will still be set to nil which explains the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)