go - 如何在 JSON 响应中包含来自 MongoDB 的 ObjectID 键?
问题描述
在 MongoDB FindOne() 函数之后,我需要获取 ObjectID 并在 http.ResponseWriter 中使用它
documento, err := bd.IntentoLogin(t.Email, t.Password)
if err != nil {
http.Error(w, "Usuario y/o Contraseña inválidos", 400)
return
}
jwtKey, err := jwt.GeneroJWT(t.Email)
if err != nil {
http.Error(w, "Ocurrió un error al intentar generar el Token correspondiente. "+err.Error(), 400)
return
}
resp := models.RespuestaLogin{
____________________________________
<<<<< Here I need to Include the ObjectID
____________________________________
Nombre: documento.Nombre,
Apellidos: documento.Apellidos,
Token: jwtKey,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(resp)
如果我需要将此 ObjectID 添加到 JSON 结构中,正确的语法是什么?
谢谢
解决方案
谢谢,但在您的示例中,您使用文字
ID: bson.ObjectId("112233"),
我需要知道如何使用 DB 对象
推荐阅读
- javascript - 为什么道具变量不能被破坏?
- swift - UITextField 必须有最少的字符才能激活 UIButton
- javascript - 循环后返回对象数组的值
- java - ListView 重叠折叠工具栏
- r - 尝试用特定年份填充缺失的数据并按顺序填充。
- hadoop - Hadoop 3.1.1 仅显示一个活动数据节点
- node.js - 如何使用 Node express 读取文本文件并将其写入 HTML 标签?
- html - 在 Github 上创建网页
- linux - 设置 ACL 权限后用户无法访问文件夹
- python-3.x - 如何在 python 循环中更新 matpplotlib 库图,该图位于 Jupyter 笔记本中的同一位置?