- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很想知道是否有更好的方法来更新字段值。我使用的字段值有时会存在,有时不会,具体取决于用户创建的帖子类型:
这是我的代码,用于列出要添加到数据库的字段
switch (req.body.postType) {
case "text":
postFields = {
postType: "text",
postBody: req.body.postBody
};
break;
case "video":
postFields = {
postType: "video",
postTitle: req.body.postTitle,
postBody: req.body.postBody,
video: {
...req.files["postVideo"][0],
thumbnail: { ...req.files["postVideoThumbnail"][0] }
}
};
break;
case "photo":
postFields = {
postType: "photo",
postTitle: req.body.postTitle,
postBody: req.body.postBody,
photo: {
...req.files["postPhoto"][0]
}
};
break;
case "document":
postFields = {
postType: "document",
postTitle: req.body.postTitle,
postBody: req.body.postBody,
document: {
...req.files["postDocument"][0]
}
};
break;
default:
return res.status(400).json({
Error:
"Incorrect postType sent to server. Must be: text, video, photo"
});
}
构建完成后,我将使用findOneAndUpdate
,如下所示
Feed.findOneAndUpdate(
{ school: req.user.school },
{
$push: {
posts: postFields
}
},
{ new: true }
)
是否有更好的方法来创建字段值?
最佳答案
你可以这样做,因为你已经在使用 ES6:
let { postType, postBody, postTitle } = req.body // <-- de-structure
let postFields = {
postType,
postBody,
postTitle, // <-- no title for text?
...(type === 'video' ? { video: {...req.files["postVideo"][0], thumbnail: { ...req.files["postVideoThumbnail"][0]}}} : {}),
...(type === 'photo' ? { photo: { ...req.files["postPhoto"][0]}} : {}),
...(type === 'document' ? { document: {...req.files["postDocument"][0]}} : {}),
}
如果text
没有标题,那么只需使用与使用ternary operator的video
等相同的方法即可。如果类型是 text
,只需通过传播带有标题的对象来添加标题...
基本上,这个想法是使用带有三元运算符的扩展来有条件地用属性修饰对象。
关于javascript - 将 findOneAndUpdate 与不同的字段值一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56367214/
我在 mongoose 的 findOneAndUpdate 中遇到问题。情况是我正在通过查找来更新文档。查询如下: UserModel.findOneAndUpdate({ individualId
以下是我的 mongo 数据库条目。 my-mongo-set:PRIMARY> db.stat_collection.find({name : /s/}) { "_id" : ObjectId("5
我正在尝试更新 mongodb 集合中的文档,但它对我不起作用。这是所有字段都到达后端的函数。我使用mlab在线mongo db router.post( '/accept', (req,re
我有一个应用程序,其中有一个教师数据库,其中包含一系列类(class) ID。创建新类(class)时,我尝试查找讲师的数据库条目并使用新类(class)的 ID 更新 classID 的数组。但是,
我正在尝试使用 findOneAndUpdate 更新存储在 mongoose 服务器中的文档之一。我将其设置为 PUT 请求,这就是我定义它的方式。 public updateConfigWithT
这个问题已经有答案了: findAndModify or findOneAndUpdate - "is not a function" (2 个回答) 已关闭 5 年前。 我花了整个早上的时间在谷歌上
我正在尝试使用 findOneAndUpdate 更新存储在 mongoose 服务器中的文档之一。我将其设置为 PUT 请求,这就是我定义它的方式。 public updateConfigWithT
该方法返回未定义。例如,在 findOneAndUpdate 解析之前完成。 exports.updateMovie = async (movie) => { try { re
尝试使用 findOneAndUpdate() 更改 MongoDB 中数组内的对象。我不清楚 mongoDB 的 nodeJS 驱动程序的文档。该文件看起来像这样: { _id:ObjectID
Tag schema结构如下: _id: "abcsd12312", nsp: "localhost.com", tags: [ 0: { tag: "#feedback",
我正在使用 Node、Express 和 Mongoose 开发 REST API。当我更新基本模型时,一切正常。但是当我在这种情况下尝试更新鉴别器对象 sportEvent 时,它不起作用。 Eve
我有一个 post 路由,它接收来自 express 应用程序中的 PUT 请求的数据,该应用程序旨在根据提交的表单输入更新 mongoose 文档。 “基本”模型是 Profile,我有两个 dis
我正在使用 mongodb 在某个集合中插入和更新一些文档。当我更新文档时,我只想获取修改后的字段。我可以通过使用 returnOriginal 选项进行更新来获取完整的新文档(包括修改后的字段),但
我正面临一个问题。我有一个名为 Car 的模型它具有以下结构。 { _id: '6179bd464c68a217e895703c', salesInfo: { salesDate: [
每当我进行更新 api 调用时,我只需要更新 serviceActiveFlag 状态。更新 api 调用后,我可以看到创建了一个包含空车辆数组的新文档,如下所示。 _id:59c76073c11d3
如果 componentId 和 wfInstanceId id 匹配,我需要更新“fields”数组中的类型、键和值。 这是我需要这样做的文档。 { "_id": { "$o
这个问题已经有答案了: mongodb: upserting: only set value if document is being inserted (4 个回答) 已关闭 5 年前。 我正在使用
我很想知道是否有更好的方法来更新字段值。我使用的字段值有时会存在,有时不会,具体取决于用户创建的帖子类型: 这是我的代码,用于列出要添加到数据库的字段 switch (req.body.postTyp
我有一个 Mongoose 模式,它有一个属性,它是 ObjectId 的数组。子文档。 如果需要删除/删除其中一个子文档,我当然想将其从父文档的数组属性中删除。 我的问题是:我以什么顺序执行子级的删
我目前正在与 Mongoose 一起工作,我遇到了一些我觉得很奇怪的事情。 我的代码如下: Challenge.findOneAndUpdate({ _id: req.params.cha
我是一名优秀的程序员,十分优秀!