【问题标题】:Amplify Schema complains because argument type is not 'List<UserWallet>?'Amplify Schema 抱怨,因为参数类型不是 'List<UserWallet>?'
【发布时间】:2021-07-09 22:40:04
【问题描述】:

我正在尝试使用 DataStore 在本地添加新数据,这是我得到的错误:

Cannot convert value of type 'User' to expected argument type 'List<UserWallet>?'

我定义了以下架构(用户和钱包之间的多对多):

type Wallet @model @auth(rules: [{allow: public}]) {
  id: ID!
  address: String
  users: [UserWallet] @connection(keyName: "byWallet", fields: ["id"])
}

type User @model @auth(rules: [{allow: public}]) {
  id: ID!
  name: String!
  UserWallets: [UserWallet] @connection(keyName: "byUser", fields: ["id"])
}

type UserWallet @model(queries: null) @key(name: "byUser", fields: ["userID", "walletID"]) @key(name: "byWallet", fields: ["walletID", "userID"]) @auth(rules: [{allow: public}, {allow: public}]) {
  id: ID!
  userID: ID!
  walletID: ID!
  user: User! @connection(fields: ["userID"])
  wallet: Wallet! @connection(fields: ["walletID"])
}

我正在尝试在official docs 之后添加一些数据,所以我尝试了这个:

let newUser = User(name: "Arturo")
let newWallet = Wallet(address: "0xewefwef32", users: newUser)


Amplify.DataStore.save(newUser) { userResult in
    switch userResult {
    case .failure(let error):
        print("Error adding post - \(error.localizedDescription)")
    case .success:
        Amplify.DataStore.save(newWallet) { newWalletResult in
            switch newWalletResult {
            case .success:
                print("User saved!")
            case .failure(let error):
                print("Error adding newWallet - \(error.localizedDescription)")
            }
        }
    }
}

但我收到了上面的错误。是什么导致该过程失败?

谢谢

【问题讨论】:

    标签: swift swiftui aws-amplify


    【解决方案1】:

    经过大量小时我找到了答案,它正在寻找架构关系名称,在这种情况下我将其命名为钱包,哦,天哪...无论如何,如果有人遇到同样的问题,这是解决方案:

    let newWallet = Wallet(address: "0xewefwef32", user: newUser.wallet)
    

    注意最后的 newUser.wallet。那是棘手的部分:)

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      相关资源
      最近更新 更多