简体   繁体   English

JS Mocha中的数据库请求异步API请求?

[英]Async api request with db request in js mocha?

I build nodejs server, and now I'm testing it with mocha. 我构建了nodejs服务器,现在我正在用Mocha对其进行测试。

I have problem with async requests. 我有异步请求问题。 I send my object to API, and then check record for with object in DB. 我将对象发送到API,然后检查数据库中对象的记录。 I need use only co library and generators. 我只需要使用co库和生成器。 There's error: 有错误:

  TypeError: Cannot read property 'id' of null

It depends on insertUser object is null, but I don't know why object from database is null. 它取决于insertUser对象是否为null,但我不知道为什么来自数据库的对象为null。

API works fine, and sequilize works fine. 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);
        });
    });

I solve my problem. 我解决了我的问题。 Code is fine, but password in db is hashing and I check hash password and order password 代码很好,但是db中的密码是哈希,我检查哈希密码和订单密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM