【发布时间】:2019-01-09 17:05:46
【问题描述】:
我是 MongoDB 的新手,我正在使用 mongoose 库来帮助我在 MongoDB 中存储数据。尽管我所有的 (mocha) 测试都通过了,但我仍然不断收到此错误:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of null
at C:\MY STUFF\CODING\Projects\mongodb tutorial\mongodb-playlist\test\finding_test.js:33:21
UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch()
DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
这是我的代码:
const assert = require('assert');
const MarioChar = require('../models/mariochar');
describe('Finding records', function(){
// this.timeout(15000);
var char;
beforeEach(function(done){
char = new MarioChar({
name: 'Mario'
});
// now test it
char.save().then(function(){
// done();
});
done();
});
it('Finds one record from the database', function(done){
// this.timeout(15000);
MarioChar.findOne({name: 'Mario'}).then(function(result){
assert(result.name === 'Mario');
// done();
});
done();
});
it('Finds one record by ID from the database', function(done){
MarioChar.findOne({_id: char._id}).then(function(result){
assert(result._id.toString() === char._id.toString());
// done();
});
done();
});
});
为了消除这些错误/警告,我尝试了所有 cmets。
当我使用mocha --trace-warnings finding_test.js 运行时,我没有收到任何警告,但是如果我使用此命令npm run test 运行,我会收到这些警告。
发生了什么事?
【问题讨论】:
标签: javascript node.js mongodb mongoose mocha.js