我不断收到错误“TypeError:无法读取未定义的属性‘resolve’”,我不知道为什么会这样。有什么想法吗?
describe('render fn', () => {
it('should return a string', () => {
let filename = __filename,
content = 'Hello, World',
theme = './test.less';
return file.render(filename, content, theme)
.then((css) => {
expect(css).to.be.a('string');
});
});
});
这是我要测试的代码:
render(filename, content, theme) {
return new Promise((resolve, reject) => {
let options = JSON.parse(JSON.stringify(this.config.renderOptions));
if (theme)
options.paths.push(path.resolve(this.config.theme.path, theme));
options.paths.push(path.resolve(this.config[filename].resolve.root));
less.render(content, options, (e, output) => {
if (e != null) reject(e);
resolve(output.css);
});
});
}
我已经为此工作了很长时间,如果有任何帮助,我将不胜感激。 谢谢!
最佳答案
I keep getting the error 'TypeError: Cannot read property 'resolve' of undefined,' and I don't know why it's happening. Any thoughts?
问题出在这一行:
options.paths.push(path.resolve(this.config[filename].resolve.root));
您尝试读取对象 this.config[filename]
上的属性 .resolve
的位置。错误消息告诉您该对象是 undefined
。您的代码等于:
var myObject = undefined;
console.log(myObject.resolve); //Cannot read property 'resolve' of undefined
我无法从您在问题中列出的代码中判断出为什么 this.config[filename]
是 undefined
。
关于javascript - 使用 Mocha 测试 Promise 时出错 - 'TypeError: Cannot read property ' resolve'of undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271563/