gpt4 book ai didi

mysql - 是否可以在 GET 请求中返回流利的 child ?

转载 作者:行者123 更新时间:2023-12-02 00:13:03 26 4
gpt4 key购买 nike

现在我有以下两个类:

final class Song: MySQLModel {
var id: Int?
var lyrics: String
var artist: String
var title: String
var strummingPattern: String

init(id: Int? = nil, title: String, lyrics: String, artist: String, strummingPattern: String) {

self.id = id
self.lyrics = lyrics
self.title = title
self.artist = artist
self.strummingPattern = strummingPattern

}
}

extension Song {
var chords: Children<Song, SongChord>{
return children(\.songID)
}
}

final class SongChord: MySQLModel {
var id: Int?
var songID: Int
var chord: String
var position: Int

init(id: Int? = nil, chord: String, position: Int, songID: Int) {
self.songID = songID
self.id = id
self.chord = chord
self.position = position
}
}

extension SongChord {
var chords: Parent<SongChord, Song>{
return parent(\.songID)
}
}

我想返回所有 Song 及其各自的 SongChord 子项的列表。到目前为止,我想出了:

return Song.query(on: req)
.join(\SongChord.songID, to: \Song.id)
.alsoDecode(SongChord.self)
.all()

但这会返回错误“无法将类型‘EventLoopF​​uture<[(Song, SongChord)]>’的值转换为闭包结果类型‘_’”。网上有人建议做平面图,但是还是报错。任何关于如何取回 child 的建议都将不胜感激。

最佳答案

因为错误提示您返回 EventLoopFuture<[(Song, SongChord)]> .如果您从 query result 解码多个模型,您将收到 Song 的 Tupel和 SongChord对于每个结果行;你现在要做的是:

  1. 创建一个符合 SongViewModel 的结构(例如 Content)并具有来自 Song 的所有相关字段和另一个名为 chords: [SongChords] 的变量.原因是,原来Song模型没有 SongChord 的数组但只有一个Children<Song, SongChord>你无法轻易解决的问题
  2. Map你的结果来自 [(Song, SongChord)][SongViewModel] .这可能有助于您转换元组数组:https://stackoverflow.com/a/31220067/710350
  3. 返回[SongViewModel]

关于mysql - 是否可以在 GET 请求中返回流利的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com