2017-07-07 67 views
0

我构建nodejs服务器,现在我正在用mocha测试它。在js mocha中使用db请求的异步api请求?

我有异步请求的问题。我将我的对象发送给API,然后检查DB中的对象的记录。我只需要使用co库和生成器。 有错误:

TypeError: Cannot read property 'id' of null 

这取决于insertUser对象是空的,但我不知道为什么从数据库对象为null。

API工作正常,并且sequilize工作正常。

it('it should POST a user',() => { 
     return co(function *() { 
      let user = { 
       name: "testInsertUser", 
       password: "12345" 
      }; 
       let res = yield chai.request(server) 
         .post('/api/users') 
         .send(user); 

       res.should.have.status(HTTPStatus.OK); 
       res.body.should.be.a('object'); 
       res.body.should.have.property('name').eql(user.name); 
       res.body.should.not.have.property('password'); 

       //Find user in db 
       let insertUser = 
        yield models.User.findOne({ 
           where: { 
            name: user.name, 
            password: user.password 
            } 
           }); 
       res.body.should.have.property('id').eql(insertUser.id); 
     }); 
    }); 

回答

0

我解决了我的问题。 代码是好的,但密码在分贝哈希和我检查哈希密码和订单密码