- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
首先要提到的是,我对 Node.Js 和 MongoDB 完全陌生。
我正在使用 Node.Js 和 MongoDB 编写后端 API,它将处理来自前端的 GET、POST、DELETE 请求,这些非常简单。
我在使用 DELETE 功能时卡住了。
这是我的 posts.service.ts 文件,其中包含此 deletePost() 函数,该函数将 postId 发送到后端 app.js文件。
`
deletePost(postId: string) {
this.http.delete('http://localhost:3000/api/posts/' + postId)
.subscribe(() => {
console.log(postId);
console.log('Deleted');
});
}
`
我已经添加了这个 console.log(postId) 来检查它是否真的包含实际的 postId 并发现它包含。我还通过 mongo shell 将其与 MongoDB 中的真实 ID 进行了匹配。
这是后端 app.js 文件中的 delete() 函数,它应该执行实际任务。
`
app.delete("/api/posts/:id", (req, res, next) => {
Post.deleteOne({ _id: req.params.id }).then(result => {
console.log(result);
res.status(200).json({message: "Post deleted"});
});
});
`
console.log(result) 行应该在终端中打印一些结果,但它没有,所以它不会删除数据库中的集合。
我在 Ubuntu 16.04 LTS 电脑上运行它。
一些线索意味着很大的帮助。非常感谢您的努力。
最佳答案
deleteOne
不会返回已删除的文档。它总是删除第一个匹配的文档,并用 bool 值返回删除的文档数。
来自 mongodb docs删除一个:
Returns:
A document containing: A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled
deletedCount containing the number of deleted documents
来自 Mongoose docs
Deletes the first document that matches conditions from the collection. Behaves like remove(), but deletes at most one document regardless of the single option.
关于javascript - 如何修复 Mongoose 模型的 deleteOne() 函数,当它不被 req.params.id 删除时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54740078/
我正在开发一个 nodejs 项目快速应用程序。我的路线看起来像这样: router.get('/observe/:fileName', function(res, req){ var req
从 Facebook 初始成功登录并使用 passport-facebook 1.0.3 和 express 4.6.1 登录重定向回调后,req.session.passport 和 req.use
req.body有什么区别和 req.params.all()何时用于风帆 Controller ? 最佳答案 req.body是从请求正文中解析出来的任何内容,例如 POST 中的有效负载要求。 r
我想知道 req.query 和 req.body 有什么区别? 下面是一段代码,其中req.query用来。如果我使用 req.body 会发生什么而不是 req.query . 以下函数被调用作为
我以前问过类似的问题,但我注意到它在 Javascript 部分。对于现在可能出现的问题,我也有更具体的想法。 基本上,req.session.passport 在我的日志中是空的。每当我开始浏览我的
我不久前从某处盗取了这段代码,并想回来进一步了解它。 iv 在 pi 上运行它已经有一段时间了,没有任何问题,但是当我运行网络服务器时,我需要恢复操作系统的备份,自从这样做以来,我一直无法让它运行,如
Req.isAuthenticated()在登录后以及注册新用户后返回false。我需要做什么来修复它? 我有一个具有两个不同登录名的应用程序。一个用于“用户”,另一个用于“雇员”。 “用户”用户是我
我一直在拼凑来自几个不同教程的代码,以使用 MEAN 堆栈构建一个基本的待办事项应用程序,使用 node、express、angular 和 mongodb。一个教程介绍了为 GET、POST 和 D
我使用以下路由接收 url 中的参数: app.get('/:lang', function(req, res) { }); 我将推送 lang 的值并将其发送到一个函数,但我希望只能在用户不输入任何
根据条件,我需要销毁用户的当前 session ,并将他重定向到带有消息的登录页面。我使用 flash 来获得只显示一次的消息。除了这里,这在我的应用程序上无处不在,因为这里我在 req.sessio
Sample 'Advanced REST Client' Request 我正在使用 Postman 和高级 REST 客户端为以下代码创建基本 POST 请求 - 'use strict'; va
if (!req.session.cart) { req.session.cart = { styles: styles,
我正在尝试使用 NodeJs 脚本将文件上传到服务器。我正在尝试以下操作。 HTML Upload a zip file
通过 req.query[myParam] 和 req.params.myParam 获取 QUERY_STRING 参数有区别吗?如果是这样,我应该什么时候使用哪个? 最佳答案 鉴于这条路线 app
在 Shiny 的应用程序中,我使用 req 检查输入的有效性如果输入的要求为 TRUE(即不为空、不为 FALSE 等),则更新输出。 如果 req 我想运行一些代码是 FALSE,即使用 req(
我正在使用 Express 和 Body Parser。给定以下标题键: X-Master-Key 当我使用下面的代码片段时,它无法输出值 req.headers['X-Master-Key'] //
要么使用正文解析器 application/x-www-form-urlencoded body parser 或 json body parser 产生相同的结果。 这就是我调用 API 的方式 $
在nodejs中我们通常会做这样的事情: req.checkBody('name', 'Group name is required.').notEmpty(); 同样,我也做了这样的事情: req.
我正在使用 Multer 在 keystone 环境中解析多部分表单,并且无法访问路由 Controller 内的 req.body 和 req.file 数据 路线/index.js var key
我想了解这是为什么: req.session._id = doc._id; req.session.id = doc._id; console.log(typeof req.session._id);
我是一名优秀的程序员,十分优秀!