Return直接用模型的数据代替字典Express.js和Mongoose

Return the data of a model directly instead of a dictionary Express.js and Mongoose

可能标题不是很清楚,有点混乱,老实说,我真的不知道怎么问这个问题,因为我是 JavaScript 和 [=45= 的新手], 所以我会尽力解释我的问题。

如果这是一个非常基本的问题或者已经有人问过,请提前致歉,但由于我真的不知道怎么问这个问题,所以我根本找不到任何答案。

我目前正在构建一个 REST API,使用 Node.js 和 Express.js 以及 Mongoose 来访问服务器数据库。我还有一个 Swift 包装器和 SwiftUI 客户端来处理这些数据。

在API中,我有以下型号:

const UserSchema = new Schema({
  id: { type: String, required: true },
  email: { type: String, unique: true, required: true, lowercase: true },
  displayName: { type: String, required: true },
  displayImage: { type: String, required: true },
  password: { type: String, required: true, select: false },
  dateCreated: { type: Date, default: Date.now() },
  lastLogin: Date,
  collections: [Collection.schema]
}, { collection: 'Usuarios' })

Swift 中的等价物如下:

public struct User: Codable, Identifiable {

    // MARK: - STORED PROPERTIES

    /// The account's ID as registered by the server.
    // swiftlint:disable:next identifier_name
    public let id: String

    /**
     The account's email.
     */
    public let email: String

    /**
     The account's display name.
     */
    public let displayName: String

    /**
     The account's display image.
     */
    public let displayImage: String

    /**
     The account's password.
     */
    public let password: String?

    /**
     The date the account was created.
     */
    public let dateCreated: String

    /**
     The date of the last login.
     */
    public let lastLogin: String

    /**
     The collections associated with this account.
     */
    public let collections: [Collection]

}

/// Grants us conformance to `Hashable` for _free_
extension User: Hashable {
    public static func == (lhs: User, rhs: User) -> Bool {
        return lhs.id == rhs.id
    }

    public func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

然后,我有以下检索用户的功能:

function getUsers (req, res) {
  User.findOne({id: req.userID}, (err, user) => {
    if (err) return res.status(500).send({message: `An error occurred while doing the petition: ${err}`})
    if (!user) return res.status(404).send({message: `No user with id ${req.userID} could be found.`})

    res.send(200, { user })
  }).select('-_id')
}

到这里为止,一切都还好。如果我向特定端点发出 post 请求,getUsers 函数是 运行 并且我 return 编辑了以下数据:

{
    "user": {
        "id": "ecd4e343-4898-4daf-aa2b-dd176011a70c",
        "email": "ena.viens@mailtag.com",
        "displayName": "Buck",
        "displayImage": "image",
        "dateCreated": "2022-04-17T22:01:53.960Z",
        "lastLogin": "2022-04-17T22:02:13.115Z",
        "collections":[],
        "__v":0
    }
}

当我将此数据解码为上面提供的 swift 模型时,问题就来了。由于 API 首先 return 是一个 user 字典,我无法将数据解码到模型中,因为它会引发错误。

我如何使 express.js(或猫鼬)return 直接成为数据而不是另一个字典?这是执行此操作的正确方法,还是我应该更改解码数据的 Swift 文件,以便它需要字典?

提前致谢。

我终于找到了这个问题的答案。一开始我用的是@jnpdx's answer by creating a Container that only had one variable that was the data model that was retrieved. I was using this approach until, after looking for examples of different apis in GitHub, I finally found the solution. It was as simple as using Express.js'res.json函数

这样可以轻松地直接发送 json 数据。

抱歉问了这个愚蠢的问题,希望能帮到一些人!