2

我尝试运行过滤 KeyProperty 的查询。我目前使用的dart gcloud包是版本:0.2.0+8

所以对于这样的模型:

@Kind(idType: IdType.String)
class Foo extends Model {
  @ModelKeyProperty(indexed: true, required: true)
  Key bar;
}

我想运行这样的查询:

Query query = new Query(db, Foo);
query.filter('bar =', someKey);
var result = query.run();

但我收到以下错误:

Uncaught error in request handler: ApplicationError: Cannot encode unsupported Key type.
#0      Codec.encodeProperty (package:appengine/src/api_impl/raw_datastore_v3_impl.dart:319:7)
#1      DatastoreV3RpcImpl.query (package:appengine/src/api_impl/raw_datastore_v3_impl.dart:527:43)
#2      Query.run.<anonymous closure> (package:gcloud/src/db/db.dart:232:28)
4

1 回答 1

2

假设

import 'package:gcloud/datastore.dart' as datastore;
import 'package:gcloud/db.dart';

在我之前的实验中,我记得必须将Key(从 db.dart)转换为datastore.Key. 我不知道为什么这个 API 期望这个,而其他 API 处理db.Key正确,所以我无法判断它是否是一个错误,但以下应该可以工作:

await db.withTransaction((Transaction transaction) async {
  // convert the key to a datastore key
  datastore.Key datastoreKey = db.modelDB.toDatastoreKey(someKey);

  // query by bar key
  Query query = transaction.query(Foo, ancestorKey);
  query.filter('bar =', datastoreKey);
  await query.run().listen((Model model) {
    print(model.key.id);
  }).asFuture();
});
于 2015-11-23T23:55:41.397 回答