Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据存储没有';不要将嵌套结构放入Go中_Go_Google Cloud Datastore - Fatal编程技术网

数据存储没有';不要将嵌套结构放入Go中

数据存储没有';不要将嵌套结构放入Go中,go,google-cloud-datastore,Go,Google Cloud Datastore,结构如下所示: type Account struct { Username string // NameKey Password []byte `datastore:",noindex"` RegistrationTime time.Time `datastore:",noindex"` AppUser } type AppUser struct { LoginEntries []LoginEntry `datastore:",

结构如下所示:

type Account struct {
  Username         string    // NameKey
  Password         []byte    `datastore:",noindex"`
  RegistrationTime time.Time `datastore:",noindex"`
  AppUser
}

type AppUser struct {
  LoginEntries []LoginEntry `datastore:",noindex"`
}

type LoginEntry struct {
  Timestamp time.Time `datastore:",noindex"`
  UserAgent string    `datastore:",noindex"`
  IP        string    `datastore:",noindex"`
}
我还确信我正确地放置了数据,因为其他数据在更新时没有问题,并且我在将
fmt.Println
帐户的内容保存到数据存储中之前尝试了
put(ctx、key和account
)当我打印它时,我可以看到所有的
AppUser
信息。但是当我以后
Get
用户时,
AppUser
信息就不存在了(只是显示为
{[]}

我很确定我以前在数据存储中存储过嵌套结构片,没有任何问题,所以我很困惑是什么导致了它


Put func:

func PutAccount(ctx context.Context, acc Account) (*datastore.Key, error) {
  if isValidUsername(acc.Username) != true {
    return nil, errors.New("Invalid username.")
  }
  var hashedPassword []byte
  if acc.RegistrationTime.IsZero() {
    var err error
    hashedPassword, err = bcrypt.GenerateFromPassword(acc.Password, 12)
    if err != nil {
      return nil, err
    }
  } else {
    hashedPassword = acc.Password
  }
  account := Account{
    Username:         strings.ToLower(acc.Username),
    Password:         hashedPassword,
    RegistrationTime: time.Now(),
    AppUser:          acc.AppUser}
  fmt.Println("PutAccount, account:", account) // before saving it prints the AppUser without problems
  key := datastore.NameKey("Account", account.Username, nil)
  return database.DatastoreClient().Put(ctx, key, &account)
}
获取函数:

func GetAccount(ctx context.Context, key *datastore.Key) (Account, error) {
  var account Account
  err := database.DatastoreClient().Get(ctx, key, &account)
  if err != nil {
    return account, err
  }
  return account, nil
}

使用命名结构工作。例如:

type Account struct {
  Username         string    // NameKey
  Password         []byte    `datastore:",noindex"`
  RegistrationTime time.Time `datastore:",noindex"`
  AppUser          AppUser
}

至于为什么匿名嵌入式结构不起作用,这可能值得一试。

请显示用于获取和放置数据的代码。您可能还想尝试使用平面结构来获取
get
,看看是否有效。您可能还想看一下@Marc请参见编辑。我也注意到了这个答案,但我发现文档相当c关于融合和复杂性,我的观点是,你需要找出哪里出了问题。这是投入还是收获?你可以通过移动到一个平面结构来缩小问题的范围,其中
LoginEntries
帐户的一个字段。使用其他工具检查数据是否确实存在也会有所帮助。嵌入式结构是平面结构有关详细信息,请参阅:非常感谢,非常感谢。它不是真正“嵌入”的。如果它被命名,现在是吗?;)