3 回答

TA貢獻1866條經驗 獲得超5個贊
讓我們首先回顧一下幕后工作的代碼,返回的是什么,然后我們一次一個地查看問題。
當我們用它調用sub.New()
方法params
時返回Subscription
類型
注意:我只會顯示類型的有限定義,因為添加完整的結構會使答案變大,而不是特定于問題上下文
讓我們看看Subscription
類型
type Subscription struct {
...
// Start of the current period that the subscription has been invoiced for.
CurrentPeriodStart int64 `json:"current_period_start"`
// ID of the customer who owns the subscription.
Customer *Customer `json:"customer"`
...
// List of subscription items, each with an attached price.
Items *SubscriptionItemList `json:"items"`
...
}
讓我們看一下第一個錯誤
cannot use result.CurrentPeriodStart (type int64) as type time.Time in assignment
根據Subscription
我們可以看到CurrentPeriodStart
的類型是類型int64
,而你試圖將它設置為類型的StartAt
字段,因為類型不同,一種類型不能分配給其他類型,為了解決這個問題,我們需要明確地將它轉換為可以這樣做:SubscriptionDetails
time.Time
time.Time
data.StartAt = time.Unix(result.CurrentPeriodStart, 0)
time.Unix
time.Time
方法從傳遞的值創建類型int64
并返回我們可以分配給StartAt
字段的類型
現在讓我們繼續第二個錯誤
cannot use result.Customer (type *stripe.Customer) as type string in assignment
正如我們從Subscription
定義Customer
字段中看到的那樣,*Customer
它不是字符串類型,因為您試圖將類型分配給*Customer
字符串CustomerId
類型的字段,這不可能導致上述錯誤,引用的數據不正確那么正確的數據在哪里正確的數據在帶有字段的*Customer
類型中可用ID
,可以按如下方式檢索
data.CustomerId = result.Customer.ID
讓我們回顧一下最后一個錯誤
result.Items.Data.price undefined (type []*stripe.SubscriptionItem has no field or method price)
同樣,如果我們查看Subscription
定義,我們可以看到Items
字段是類型*SubscriptionItemList
類型,如果我們查看*SubscriptionItemList
定義
type SubscriptionItemList struct { APIResource ListMeta Data []*SubscriptionItem `json:"data"`}
它包含一個字段名稱Data
,Data
類型[]*SubscriptionItem
注意它是 slice []
of *SubscriptionItem
,這是錯誤的原因,因為Data
字段是 slice of*SubscriptionItem
我們可以解決問題如下:
data.PricePerSeat = result.Items.Data[0].price.UnitAmount
我想指出的可能發生的錯誤很少,請繼續閱讀下面的內容以解決這些問題
現在讓我們看看*SubscriptionItem
定義
type SubscriptionItem struct { ... Price *Price `json:"price"` ... }
它包含Price
名稱以大寫字母開頭的字段通知,在共享代碼中它以小寫字母引用,這可能會導致另一個問題,最后如果我們查看Price
定義
type Price struct { ... // The unit amount in %s to be charged, represented as a whole integer if possible. Only set if `billing_scheme=per_unit`. UnitAmount int64 `json:"unit_amount"` // The unit amount in %s to be charged, represented as a decimal string with at most 12 decimal places. Only set if `billing_scheme=per_unit`. UnitAmountDecimal float64 `json:"unit_amount_decimal,string"` ... }
它包含UnitAmount
我們正在使用的字段,但這里有一個問題UnitAmount
是類型,int64
但類型PricePerSeat
是類型,float64
將不同的類型分配給彼此會再次導致錯誤,因此您可以轉換int64
為float64
或者更好的是您可以使用包含的類型中UnitAmountDecimal
可用的字段格式Price
相同的數據float64
將減少我們在使用UnitAmount
字段時必須進行的顯式轉換,因此根據我們得到以下解決方案的解釋
data.PricePerSeat = result.Items.Data[0].Price.UnitAmountDecimal

TA貢獻1829條經驗 獲得超7個贊
查看您提到的3個錯誤
cannot use result.CurrentPeriodStart (type int64) as type time.Time in assignment
的類型result.CurrentPeriodStart
是 int64 并且您試圖將其設置為類型的字段time.Time
,這顯然會失敗。 API 以 unix 格式發送時間,您需要對其進行解析以將其轉換為 time.Time。對其他時間字段也執行此操作
data.StartAt = time.Unix(result.CurrentPeriodStart, 0)
cannot use result.Customer (type *stripe.Customer) as type string in assignment
與上述類似的問題,當您嘗試將其設置為 type 的字段時,該字段是 typeresult.Customer
的。客戶 ID 是結構中的一個字段*stripe.Customer
string
Customer
data.CustomerId = result.Customer.ID
result.Items.Data.price undefined (type []*stripe.SubscriptionItem has no field or method price)
stripe.SubscriptionItem
結構沒有字段price
。我不確定你在這里想要什么。我建議閱讀訂閱對象文檔。

TA貢獻1735條經驗 獲得超5個贊
訪問價格result.Items.Data[0].Price.UnitAmount
如果您使用調試器,只需sub.New
在行后放置一個斷點并探索結果變量的內容
- 3 回答
- 0 關注
- 163 瀏覽
添加回答
舉報