英文:
KeysOnly Function dont return Keys
问题
我用GAE
做了一些实验,但现在遇到了一个问题。首先,我使用NewIncompleteKey
将一些内容存储到datastore
中。
问题在于,我的网站将时间戳(我将它们视为“ID”)发送到后端。然后我解析它们并希望从datastore
中删除它们。
我以为我可以这样做。
type Food struct{
Id int64
Course string
Name string
Date string
Price float64
}
...一些代码...
func deleteEntries(mealsID []string, r *http.Request) int{
// 从请求中获取上下文
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)
}
但是它不起作用,因为在datastore.Delete()
函数处出现错误。有人有什么想法吗?
编辑:
第一部分:
keys, err := q.GetAll(c, nil)
…
err = datastore.DeleteMulti(c, keys)
感谢Dave。
第二部分:
我将一个字符串作为过滤器值传递给查询,但它必须是与数据存储中的类型相同的Int64。注意:你必须将相同类型的变量也传递给查询。
func deleteEntries(mealsID []string, r *http.Request) int{
// 从请求中获取上下文
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)
}
英文:
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
得分: 4
从GetAll
返回键。所以你应该改为这样写:
keys, err := q.GetAll(c, nil)
…
err = datastore.DeleteMulti(c, keys)
GetAll
对于仅键请求会忽略dst
参数 - datastore reference。所以,在上面的示例中,key
仍将设置为nil
,这解释了错误。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论