mongoose中的exec()有什么用?
是什么?
.exec() 和 .save() 一样是 Mongoose 的异步操作,都返回一个 thenable 。
怎么用?
我们先定义一个 query 对象: const query = MyModel.findOne({}),接下来我们一般会这么使用:
query.then(doc => {...})
// or
const doc = await query.exec()
有什么区别?
query 本身并不是 Promise 对象,它只是实现了
.then和.catch方法便于异步操作;.exec()返回了一个真正的 Promise 对象。
但 query 提供了 .then 方法,我们一样可以使用 async/await :
const doc = await query
// or
const doc = await query.exec()
以上两种写法已近乎一致,而事实上它们在功能执行上也 完全一致 。
是否需要使用 .exec() ?
官方建议使用 .exec() ,因为它在使用 async/await 时提供了更好的堆栈跟踪。
以官方示例:
const doc = await Band.findOne({ name: "Guns N' Roses" }); // works
const badId = 'this is not a valid id';
try {
await Band.findOne({ _id: badId });
} catch (err) {
// Without `exec()`, the stack trace does **not** include the
// calling code. Below is the stack trace:
//
// CastError: Cast to ObjectId failed for value "this is not a valid id" at path "_id" for model "band-promises"
// at new CastError (/app/node_modules/mongoose/lib/error/cast.js:29:11)
// at model.Query.exec (/app/node_modules/mongoose/lib/query.js:4331:21)
// at model.Query.Query.then (/app/node_modules/mongoose/lib/query.js:4423:15)
// at process._tickCallback (internal/process/next_tick.js:68:7)
err.stack;
}
try {
await Band.findOne({ _id: badId }).exec();
} catch (err) {
// With `exec()`, the stack trace includes where in your code you
// called `exec()`. Below is the stack trace:
//
// CastError: Cast to ObjectId failed for value "this is not a valid id" at path "_id" for model "band-promises"
// at new CastError (/app/node_modules/mongoose/lib/error/cast.js:29:11)
// at model.Query.exec (/app/node_modules/mongoose/lib/query.js:4331:21)
// at Context.<anonymous> (/app/test/index.test.js:138:42) <--- here
// at process._tickCallback (internal/process/next_tick.js:68:7)
err.stack;
}
可以看到,使用 .exec() 时能直接追踪到异常位置: index.test.js:138:42 。
参考:
mongoose中的exec()有什么用?的更多相关文章
- Mongoose中关联查询populate的使用
MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...
- 正则表达式中的exec和match方法的区别
正则表达式中的exec和match方法的区别 字符串的正则方法有:match().replace().search().split() 正则对象的方法有:exec().test() 1.match m ...
- python中eval, exec, execfile,和compile [转载]
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果. 同样地, exec语句将字符串str当成有效Python代码来执行. ...
- [原译]在mongoose中对Array Schema进行增删改
原文地址: http://tech-blog.maddyzone.com/node/add-update-delete-object-array-schema-mongoosemongodb 本文为上 ...
- js正则表达式中test,exec,match方法的区别说明
js正则表达式中test,exec,match方法的区别说明 test test 返回 Boolean,查找对应的字符串中是否存在模式.var str = "1a1b1c";var ...
- mongoose中connect()、createConnection()和connection的区别和作用
转文:原文 1 mongoose简介 在使用mongodb数据库开发项目中,nodejs环境下可能会使用到mongoose模块连接并操作mongodb数据库.mongoose模块相当于Java中的数据 ...
- Python中的exec、eval使用实例
Python中的exec.eval使用实例 这篇文章主要介绍了Python中的exec.eval使用实例,本文以简洁的方式总结了Python中的exec.eval作用,并给出实例,需要的朋友可以参考下 ...
- find中的-exec参数
1.find中的-exec参数 在当前目录下(包含子目录),查找所有txt文件并找出含有字符串"bin"的行 find ./ -name "*.txt" -ex ...
- mongoose中的流查询stream query
mongoose中的流查询stream query,功能类似于php中的mysql_fetch_array,每次从集合中获取一条记录(文档) var cursor = Person.find({ oc ...
- mongoose中给字段添加索引的方法
mongoose中给字段添加索引的方法有两种,一种通过在定义schema的时候配置,如: var animalSchema = new Schema({ name: String, type: Str ...
随机推荐
- CSS – Sass & SCSS
前言 CSS 代码多了就不好管理了, 这是它语法先天的不足. Sass 就是加强它语法的, Sass 为 CSS 引入了一些 JS 语言的特性, 比如 variable, function, para ...
- Vue3.5中解构props,让父子组件通信更加丝滑
前言 在Vue3.5版本中响应式 Props 解构终于正式转正了,这个功能之前一直是试验性的.这篇文章来带你搞清楚,一个String类型的props经过解构后明明应该是一个常量了,为什么还没丢失响应式 ...
- ant-design-vue 登录表单校验
最近刚刚上手了 Vue3 的 antdv ui框架,来做个简单的登录校验练练手 安装 antdv 依赖 npm install ant-design-vue --save 在 main.ts/js 中 ...
- foobar2000 v2.1.5 汉化版(更新日期:2024.09.08)
foobar2000 v2.1.5 汉化版 -----------------------[软件截图]---------------------- -----------------------[软件 ...
- 28. 找出字符串中第一个匹配项的下标 Golang实现
题目描述: 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始).如果 needle 不是 hay ...
- v-if 为什么不能和 v-for 一起使用 ?
当 Vue 处理指令时,v-for 比 v-if 具有更高的优先级,通过v-if 移动到容器元素,不会再重复遍历列表中的每个值.取而代之的是,我们只检查它一次,且不会在 v-if 为否的时候运算 v- ...
- 什么是 Ajax,Ajax 的原理,Ajax 都有哪些优点和缺点
ajax是异步的js和xml,是一种创建交互式网页的开发技术,是和服务器进行异步通讯的技术 : 核心就是使用XMLHttpRequest向服务器发送请求获取数据 : 优点: 页面不需要刷新,用户体验良 ...
- 如何对 Vue 首屏加载实现优化 ?
首屏加载优化是对于 SPA 来说的 ,首次加载所有的 html css js 所需的文件 ,后面就不会因为用户对页面的操作而跳转页面 ,没有跳转页面如何展示不同的内容呢 ? 使用 Vue 的路由机制 ...
- 63.CDN优化
虽然CDN引入组件库可以优化项目,减轻服务器负载,但是在真实的项目开发中不推荐使用CDN : 因为: 1. 使用第三方服务器不稳定 2. 需要后端配置 3. 要知道组件库的全局变量名
- BUUCTF BUU SQL COURSE 1
方法一:手工注入 启动靶机 寻找注入点,使用burp抓包得到传参页面 得到注入点 /backend/content_detail.php?id=1 用and 1 =1 和 and 1 =2 判断存在s ...