- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 es6 promise 与 superagent 一起使用。我正在尝试调用一个包含 super 代理请求的函数。
Request.post(buildReq).then(res => {
if (res.ok) {//process res}
});
static post(params) {
superagent
.post(params.url)
.send(params.payload)
.set('Accept', 'application/json')
.end((error, res) => {
return this.Promise.resolve(res);
})
.bind(this);
}
enter code here Uncaught TypeError: Cannot read property 'then' of undefined
static post(params) {
return Promise.resolve(superagent
.post(params.url)
.auth(params.auth.username, params.auth.password)
.send(params.payload)
.set('Accept', 'application/json')
.end((error, res) => {
return this.Promise.resolve(res);
})
);
}
最佳答案
您从 end
返回什么并不重要方法回调,因为它在您获得响应时异步执行,并且没有使用回调执行的结果。看 here和 here在源代码中。 end
方法返回 this
,所以在你的第二个例子中你正在解析 superagent
没有回应。要获得回复您的 post
方法必须如下所示:
static post(params) {
return new Promise((resolve, reject) => {
superagent
.post(params.url)
.auth(params.auth.username, params.auth.password)
.send(params.payload)
.set('Accept', 'application/json')
.end((error, res) => {
error ? reject(error) : resolve(res);
});
});
}
关于ecmascript-6 - promise es6 和 superagent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27967000/
我试图上传多个文件并将它们发送到 Cloudinary 的 API。我循环浏览每个图像,附加它然后上传它。但是,我的代码循环遍历图像,然后再次上传第一个图像以获取文件数量: let uploadReq
我试图通过 superagent 一次上传多个文件.我知道 docs说要使用多个 .attach(),但我无法让它动态工作。 是否可以遍历数组并重复 .attach()?还是以其他方式完成? 像这样:
我在 TypeScript 项目中使用 superagent 并安装了 @types/superagent 但我看到一个我不明白的类型错误。鉴于以下... const myRequest = requ
所以我想用winston记录我的应用程序发出的所有请求。我通过 superagent 发出所有 API 请求,我基本上想设置某种中间件,例如: // Global error middleware s
React 的 superagent 库有一个文件吗?它的 github 站点有一个巨大的包,我确信我不需要所有这些。在我之前的项目中,我们只有一个文件,它负责发出 XHR 请求的所有工作。但我现在丢
我正在尝试组装一个基于 supertest 的集成测试套件(由 Mocha 运行),它会 ping 我们的 REST API 并验证响应。 但是,我的测试似乎没有按预期运行: var assert =
我正在从服务器返回 400 代码,所以我需要检查我的请求有什么问题。如何打印请求的原始文本? 这就是我现在的代码: request.post('/api/events/') .send(prepare
我正在尝试从浏览器将字符串作为文件发布,如 SO question 中所述。 但我想使用 superagent 来做到这一点。我尝试了以下方法: var request = require('supe
我正在尝试编写一个测试,其中 SuperAgent 调用多个(子)域,其中应在它们之间共享 cookie。因此,我想动态切换代理,即我无法创建新代理,因为我希望代理始终保留 cookie。 age
我正在尝试使用 SuperAgent .data() to construct a query string per the docs .但是 .data() 似乎不再存在。 superagent
如何使用 Superagent但使用 Promises A+ 规范?我正在开发一个同时使用 Superagent 和 Bluebird 的项目我想使用 .then() 语法,但如果不编写我自己的包装代
我正在使用 superagent,在检查网络时我注意到 superagent 正在为每个请求创建一个新的 TCP 连接。我将 superagent 用于一系列请求,这会导致大量 TCP 连接(可能达到
我正在为一个应用程序使用 superagent ajax 库,我正在尝试为它编写一些单元测试。我有一个看起来像这样的类(class): someClass = { getData: functio
我正在使用 ES5 和 React。我的 xhrHandler.js 的 React.creatClass 编码如下: var XhrHandler = React.createClass({
我正在开发这个 API React 应用程序。我正在使用 Superagent 获取 API 请求。但是当没有响应时我无法处理错误。我得到这个 Uncaught (in promise) Error:
我正在尝试计算文件上传的进度,所以我使用了 Superagent .我能够获得文件上传的进度。 现在,当用户选择取消按钮时,我需要中断或取消发布请求。有什么办法可以做到这一点。以下是我的代码: var
我对在 Javascript 中导入文件的多种方式有点困惑(脚本标签、RequireJS、组件 - 如果这是包含文件的另一种方式)。在我的项目中,我将 AMD 与 RequireJS 结合使用。 现在
摘自 superagent 网站,它说“有了这个技巧,您可以开发前端而无需访问任何 API。它非常有用,可以在您的计算机上本地工作,无需互联网,或者使您的功能独立于任何第三方进行测试。”
我正在尝试创建一个 multer通过 superagent 将流式文件从客户端传输到第三方的中间件. const superagent = require('superagent'); const m
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我是一名优秀的程序员,十分优秀!