node.js - Database timing issue when testing express routes -


i'm trying test express route creates record in db using bookshelf.

router.post('/', function(req, res) {   thing     .forge({       name: req.body.name     })     .save()     .then((thing) => {       res.status(201).json({         thing: thing.tojson()       });     }) }); 

to test route, i'm making requests superagent, reading returned thing id response body, looking thing in database check exists.

describe('post /', function() {   it('creates things', function(done) {     request(app)       .post('/')       .send({         name: 'my thing'       })       .end(function(err, res) {         // res.body.thing exists , has id set.         console.log("end occurred", res.body.thing.id);          thing           .where('id', res.body.thing.id)           .fetch()           .then(function(thing) {             // @ point, thing null when expect             console.log("canvas fetched", thing);           })       });   }); }); 

this looks me database timing issue because thing gets created (at least, has id when it's returned in response). can't figure out how debug singe i'm new nodejs. don't seem have sql statements being logged.

any suggestions?

i haven't test this, can try it.

new thing({'id': req.params.id}) .fetch() .then(function (thins) {  }); 

Comments