【问题标题】:How to Get "_id" field value from kinvey.com如何从 kinvey.com 获取“_id”字段值
【发布时间】:2014-10-14 08:26:26
【问题描述】:

我一直在使用 kinvey.com,每次尝试获取 Manga._id 时,它都会返回 null。你能帮我找出原因吗?

TManga = class
  strict private
    FSite,
    FManga,
    FID: String;
  published
    property Site        : string  read FSite        write FSite;
    property Manga       : string  read FManga       write FManga;
    property _id         : string  read FID          write FID;

/////////////////////////////////////// ///////////////////////////p>

var
  Mangas: TBackendObjectList<TManga>;
  Manga : TManga;
  QueryStr: TArray<string>;
  i: Integer;
begin
with xQuery do
  begin
    Execute;
    Mangas := TBackendObjectList<TManga>.Create;

    QueryStr := TArray<string>.Create('');

    xStorage.Storage.QueryObjects<TManga>('xxxx' ,QueryStr ,Mangas);

    with xListBox do
    begin
      Items.BeginUpdate;
      try
        Items.Clear;
        for I := 0 to Mangas.Count -1 do
        begin
          Manga := Mangas.Items[I];
          items.add(Manga.Site + ' - ' + Manga._id) // Manga._id this is everytime null 

        end;

      finally
        Items.EndUpdate;
      end;

    end;
  end;

http://i.hizliresim.com/M94QPN.png

【问题讨论】:

  • 实际创建对象的代码在哪里?您还可以使用For Manga in Mangas do 这样您就不必每次都将mangasi.items[I] 分配给Manga。其他属性的值是否正确?
  • 我在 xStorage.Storage.QueryObjects('xxxx' ,QueryStr ,Mangas);什么时候,我想,你填写数据......
  • @TeunPronk 感谢“For in 子句”。和其他每个属性的真实值,但只有 Manga._id 每次都返回 null。
  • @Zam "xStorage.Storage.QueryObjects('xxxx' ,QueryStr ,Mangas)" 不要在这里指定问题,否则我找不到
  • @user3825157 如果您正在调试,所有属性都会正确设置吗?你能把你的代码贴在你创建对象的地方吗?您在代码中没有看到问题,但也许其他人看到了:)

标签: delphi backend delphi-xe6 kinvey


【解决方案1】:

您是否尝试在 QueryStr 的数组元素之一中使用值“fields=_id”?

【讨论】:

  • 您可能还需要添加其他字段,以防止它们为空
  • 对不起,我实际上想将其添加为评论,而不是答案。
【解决方案2】:

您的 _id 列始终为空,因为 Kinvey API 不会将 _id 列作为简单列,而是作为记录的对象 ID。 为了获取您的漫画记录的对象 ID,您必须添加如下变量:

  oEntity: TBackendEntityValue;

所以就在“for”语句的这一行下面:

  Manga := Mangas.Items[I];

您可以添加以下两个新行:

oEntity := FBackendList.EntityValues[Manga]; // Gets the Kinvey object
Manga._id := oEntity.ObjectID; // Sets the _id property of the current TManga instance

您可能要记住的一件重要事情是何时在您的 Kinvey 收藏中添加新记录。在创建新记录之前,您不必写入新 TManga 项目的 _id 属性。但是,您需要在插入新记录后立即从 Kinvey 获取它。 此代码改编自 Embarcadero 的 ToDo 示例:

procedure TDataModule1.AddBackendItem(const AItem: TManga);
var
  oEntity: TBackendEntityValue;
begin

  // After the execution of this command the new record will be inserted in Kinvey, and the variable oEntity will get the respective object ID 
  BackendStorage1.Storage.CreateObject<TManga>(
    TMangaNames.BackendClassname, AItem, oEntity);

  AItem._id := oEntity.ObjectID; // Updates the property _id of the current instance of TManga

  FBackendList.Add(AItem, oEntity);

end;

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2011-12-22
    • 2015-12-22
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多