标签: superagent

未捕获的错误:引发了跨源错误。React 无权访问开发中的实际错误对象

每次提交区域时,它都会显示此错误:

'未捕获的错误:引发了跨源错误。React 无权访问开发中的实际错误对象”

它仅在我按下提交区域按钮时发生,我猜当旧状态更改为新状态时会发生这种情况。 (this.setState)

创建区域.js

import React, { Component } from "react";

export default class CreateZone extends Component {
  constructor(props) {
    super(props);
    this.state = {
      zone: {
        name: "",
        zipCode: "",
      },
    };
  }

  updateZone(event) {
    let updated = Object.assign({}, this.state.zone);
    updated[event.target.id] = event.target.value;
    this.setState({
      zone: updated,
    });
  }

  submitZone(event) {
    console.log("SubmitZone: " + JSON.stringify(this.state.zone));
    this.props.onCreate(this.state.zone);
  }

  render() {
    return (
      <div>
        <input
          onChange={this.updateZone.bind(this)}
          className="form-control"
          id="name"
          type="text"
          placeholder="Name"
        />{" "}
        <br />
        <input
          onChange={this.updateZone.bind(this)}
          className="form-control"
          id="zipCode"
          type="text"
          placeholder="Zip Code" …
Run Code Online (Sandbox Code Playgroud)

javascript node.js superagent reactjs

43
推荐指数
8
解决办法
8万
查看次数

如何使用superagent/supertest链接http调用?

我正在使用supertest测试一个快速API.

我无法在测试用例中获得多个请求以使用supertest.以下是我在测试用例中尝试过的内容.但测试用例似乎只执行最后一次调用,即HTTP GET.

it('should respond to GET with added items', function(done) {
    var agent = request(app);
    agent.post('/player').type('json').send({name:"Messi"});
    agent.post('/player').type('json').send({name:"Maradona"});
    agent.get('/player').set("Accept", "application/json")
        .expect(200)
        .end(function(err, res) {
            res.body.should.have.property('items').with.lengthOf(2);
            done();
    });
);
Run Code Online (Sandbox Code Playgroud)

我在这里缺少的任何东西,还是有另一种方式将http调用与superagent链接?

node.js express superagent supertest

39
推荐指数
4
解决办法
2万
查看次数

你怎么能用超级饼干?

我正在使用express这样的cookie进行cookie会话管理:

req.session.authentication = auth;
Run Code Online (Sandbox Code Playgroud)

我用类似的东西验证了经过身份验证的网址

if(!req.session.authentication){res.send(401);}
Run Code Online (Sandbox Code Playgroud)

现在,我要建与摩卡的URL测试的SuperAgent应该,但我似乎无法找到一种方式来获得/设定SuperAgent的饼干.我甚至尝试在经过身份验证的测试之前请求登录,但它无法正常工作,

我已经尝试将请求添加到mocha BDD套件的before语句中的登录,但是它仍然告诉我该请求是未经授权的,我已经测试了执行来自浏览器的请求的身份验证,但它无法正常工作套房任何想法为什么?

mocha.js node.js superagent

33
推荐指数
4
解决办法
3万
查看次数

承诺错误:对象作为React子项无效

我试图使用用户代理将json设置为一个状态,我得到错误:

Uncaught Invariant Violation:对象无效作为React子对象(找到:带有键{...}的对象).如果您要渲染子集合,请使用数组,或使用React附加组件中的createFragment(object)包装对象.

设置状态的方法:
 getInitialState: function(){
    return {
        arrayFromJson: []
    }
},

loadAssessmentContacts: function() {
    var callback = function(data) {
        this.setState({arrayFromJson: data.schools})
    }.bind(this);

    service.getSchools(callback);
},

componentWillMount: function(){
    this.loadAssessmentContacts();
},

onTableUpdate: function(data){

    console.log(data);

},

render: function() {

    return (
        <span>{this.state.arrayFromJson}</span>
    );
}
Run Code Online (Sandbox Code Playgroud) 服务
getSchools : function (callback) {
        var url = 'file.json';
       request
            .get(url)
            .set('Accept', 'application/json')
            .end(function (err, res) {
                if (res && res.ok) {
                    var data = res.body;
                    callback(data);

                } else {
                    console.warn('Failed to load.');
                }
            });
    }
Run Code Online (Sandbox Code Playgroud) JSON …

superagent reactjs

30
推荐指数
2
解决办法
8万
查看次数

webpack dev服务器CORS问题

webpack-dev-server v1.10.1用来提升我的Redux项目,我有以下选项:

contentBase: `http://${config.HOST}:${config.PORT}`,
quiet: false,
noInfo: true,
hot: true,
inline: true,
lazy: false,
publicPath: configWebpack.output.publicPath,
headers: {"Access-Control-Allow-Origin": "*"},
stats: {colors: true}
Run Code Online (Sandbox Code Playgroud)

在JS中,我使用requestfrom superagent来生成HTTP GET调用

request
          .get(config.APIHost + apiUrl)
          .set('Accept', 'application/json')
          .withCredentials()
          .end(function (err, res) {
                if (!err && res.body) {
                    disptach(() => {
                        return {
                            type: actionType || GET_DATA,
                            payload: {
                                response: res.body
                            }
                        }
                    });
                }
          });
Run Code Online (Sandbox Code Playgroud)

但是我收到了CORS错误:

XMLHttpRequest cannot load http://localhost:8000/api/getContentByType?category=all. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5050' …
Run Code Online (Sandbox Code Playgroud)

cors superagent reactjs webpack-dev-server

29
推荐指数
4
解决办法
4万
查看次数

如何使用node.js superagent发布multipart/form-data

我试图将我的superagent post请求中的内容类型发送到multipart/form-data.

var myagent = superagent.agent();

myagent
  .post('http://localhost/endpoint')
  .set('api_key', apikey)
  .set('Content-Type', 'multipart/form-data')
  .send(fields)
  .end(function(error, response){
    if(error) { 
       console.log("Error: " + error);
    }
  });
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:TypeError:参数必须是一个字符串

如果我删除:

.set('Content-Type', 'multipart/form-data')
Run Code Online (Sandbox Code Playgroud)

我没有得到任何错误,但我的后端正在接收内容类型的请求:application/json

如何强制内容类型为multipart/form-data,以便我可以访问req.files()?

node.js superagent

21
推荐指数
3
解决办法
3万
查看次数

在node.js服务器上使用supertest/superagent读取响应输出缓冲区/流

我正在尝试编写一个测试,检查API路由是否输出具有正确内容的ZIP文件.

我正在使用mocha和supertest进行测试,我想实际读取输出流/缓冲区,读取zip文件内容并查看内容是否正确.

任何想法我该怎么做?当我尝试阅读时res.body,它只是一个空物体.

  request(app)
    .get( "/api/v1/orders/download?id[]=1&id=2" )
    .set( "Authorization", authData )
    .expect( 200 )
    .expect( 'Content-Type', /application\/zip/ )
    .end( function (err, res) {
      if (err) return done( err );

      console.log( 'body:', res.body )

      // Write the temp HTML file to filesystem using utf-8 encoding
      var zip = new AdmZip( res.body );
      var zipEntries = zip.getEntries();

      console.log( 'zipentries:', zipEntries );

      zipEntries.forEach(function(zipEntry) {
        console.log(zipEntry.toString()); // outputs zip entries information
      });

      done();
    });
Run Code Online (Sandbox Code Playgroud)

zip mocha.js node.js superagent supertest

20
推荐指数
3
解决办法
1万
查看次数

确保Express App在每次Mocha测试之前运行

我正在使用ExpressJS,NodeJS,Mongoose和Mocha开发REST API.

问题是我有一个app.coffee文件,负责设置ExpressJS并连接到Mongoose.我设置它的方式是首先连接Mongoose,如果通过,则启动ExpressJS App.

问题是,在设置Mocha时,我需要确保app.coffee中存在的ExpressJS App在执行任何测试用例之前已成功完成,包括所有异步代码.

为此,我创建了一个test_helper.coffee,并在其中放置了以下代码,但是,即使app.coffee中的代码没有完全完成其实际有意义的代码,测试用例也会开始执行:

before (done) ->
  require(__dirname + '/../src/app')
  done()
Run Code Online (Sandbox Code Playgroud)

简而言之,我想确保ExpressJS应用程序在执行任何测试用例之前已完全完成其设置.

我怎么能这样做?

mocha.js mongoose node.js express superagent

19
推荐指数
2
解决办法
1万
查看次数

superagent和nock如何一起工作?

在node.js中,我很难让superagent和nock一起工作.如果我使用请求而不是superagent,它可以完美地运行.

以下是superagent无法报告模拟数据的简单示例:

var agent = require('superagent');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

agent
  .get('http://thefabric.com/testapi.html')
  .end(function(res){
    console.log(res.text);
  });
Run Code Online (Sandbox Code Playgroud)

res对象没有'text'属性.有些不对劲.

现在,如果我使用请求做同样的事情:

var request = require('request');
var nock = require('nock');

nock('http://thefabric.com')
  .get('/testapi.html')
  .reply(200, {yes: 'it works !'});

request('http://thefabric.com/testapi.html', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body)
  }
})
Run Code Online (Sandbox Code Playgroud)

模拟的内容正确显示.

我们在测试中使用了superagent,所以我宁愿坚持下去.有谁知道如何使它工作?

谢谢你,Xavier

javascript request node.js superagent nock

16
推荐指数
1
解决办法
3715
查看次数

如何在SuperAgent的每个请求中注入自定义HTTP标头?

显然,SuperAgent支持自定义HTTP标头:

request
   .post('/api/pet')
   .send({ name: 'Manny', species: 'cat' })
   .set('X-API-Key', 'foobar')
   .set('Accept', 'application/json')
   .end(function(err, res){
     if (res.ok) {
       alert('yay got ' + JSON.stringify(res.body));
     } else {
       alert('Oh no! error ' + res.text);
     }
   });
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 如果我通过npm下拉SuperAgent,我如何在SuperAgent的所有请求中注入我自己的HTTP头?
  • 注意:我完全愿意创建一个新的npm包,如果需要,可以扩展SuperAgent.

http-headers npm superagent

13
推荐指数
2
解决办法
1万
查看次数