samedi 9 juillet 2016

Can't find user through ID

I can't find a user through ID in the end method. However I can find a user outside the it method. But then I can't use the res.body._id value to give it as a argument to the find method.

Below you can find my code:

describe("A user registers", function () {
    var id = "asd";
    it('should create a SINGLE user on /api/register POST', function (done) {
        //calling REGISTER api
        server
                .post('/api/register')
                .send({
                    name: "John Doe",
                    username: "john",
                    password: "open",
                    confirm_password: "open"
                })
                .expect("Content-type", /json/)
                .expect(200)
                .end(function (err, res) {
                    //TO DO: compare created_at and updated_at
                    var data = {
                        _id: res.body._id, //the _id is dynamic because it's just created
                        name: "John Doe",
                        username: "john",
                        admin: false
                    };
                    res.status.should.equal(200);
                    assert.deepEqual(res.body, data);
                    //TO DO: check if user is inserted in the database through using the ID to find the user
                    id = res.body._id;
                    done();
                });
    });
    User.find({username: "john"}, function (err, users) {
        if (err)
            return console.error(err);
        console.log(users);
        assert.equal(users.length, 1);
    });
});

Also putting moving the find method inside the end method won't work. Because I don't see anything when I use: console.log(users);

Can someone maybe help me, please?

Aucun commentaire:

Enregistrer un commentaire