【发布时间】:2019-04-09 06:15:26
【问题描述】:
所以我正在尝试在这里进行 Mocha 测试,但我的返回值(就在 .then 之前)以 Null 形式出现。
当我在控制台记录 Store.findById(resStore.id) 时,我在日志中得到了值,但是当我返回它并将它传递给 then 时,它显示 Null?
it('should return posts with right fields', function() {
let resStore;
return chai.request(app)
.get('/stores')
.then(function(res) {
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.be.a('array');
expect(res.body).to.have.lengthOf.at.least(1);
res.body.forEach(function(store) {
expect(store).to.be.a('object');
expect(store).to.include.keys('_id', 'name', 'storeLogo');
});
resStore = res.body[0];
console.log(Store.findById(resStore.id)); // Response is QUERY Result
return Store.findById(resStore.id);
})
.then(doc => {
console.log(`Casey ${doc}`); //Response is Casey Null
resStore.name.should.equal(doc.name);
resStore.name.should.equal(doc.storeLogo);
});
});
});
【问题讨论】:
-
我希望
Store.findById返回一个 Promise,而不是查询结果。如果返回后者,则表明该方法是同步的。 -
我不认为查询是正确的词,但它返回结果,而不是 null。即:查询 { _mongooseOptions: {}, _transforms: [], mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false, '$wasForceClosed': undefined }, name: 'stores',collectionName:'stores',conn:NativeConnection { ......
-
这是一个 Mongoose 查询对象。它不代表一个查询result,只是一个查询。您尝试运行的查询显然不会产生任何结果。
-
我想,我不明白我如何将它记录在它的正上方,但是当我返回它时,它是空的?
标签: node.js mongoose mocha.js chai