关于javascript:无法读取未定义的属性\\’状态\\’-摩卡柴

Cannot read property 'status' of undefined - Mocha Chai

因此我在Node.js环境中使用了摩卡咖啡。
我不明白为什么我在运行mochajs时无法获得响应状态。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('server');
let expect = require("chai").expect;
let should = require("should");

let request = require("superagent");
let util = require("util");

chai.use(chaiHttp);

describe('API Clinic Test', function() {

  it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

  it('should list a SINGLE clinic on /api/v1/clinic/<id> GET');
  it('should add a SINGLE clinic on /api/v1/clinic POST');
  it('should update a SINGLE clinic on /api/v1/clinic/<id> PUT');
  it('should delete a SINGLE clinic on /api/v1/clinic/<id> DELETE');
});

每次我运行mocha test.js时,我总是收到此错误消息:

Uncaught TypeError: Cannot read property 'status' of undefined

哦,我也使用了方法。
我收到了另一个错误消息,例如:无法读取属性应该为空

我在此线程上阅读。

应该js无法读取null的属性"应该"

这就是为什么我要更改并使用Expect方法的原因。

你们能帮我吗?

谢谢。

:::更新:::
如何解决该问题?
而不是使用以下代码行:

1
2
3
4
5
6
7
8
9
it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

我用这个:

1
2
3
4
5
6
7
8
it('should list ALL clinic on /api/v1/clinic GET', function(done) {
chai.request('localhost:5000') .get('/api/v1/clinic')
.end(function(err, res){
            // res.should.have.status(200);
            expect(res.status).to.equal(200);
          done();
        });
      });


在我的情况下,我忘记了导出export defaul mymodule之类的模块。所以检查一下。


您很可能遇到错误...您应该使用类似于以下内容的行

1
if(err) done(err);

每条评论...这将您引向正确的方向。此外,您需要执行以下操作:

1
chai.request('http://localhost:5000').get('/api/v1/clinic')