javascript - 使用 Mocha 测试 Promise 时出错 - 'TypeError: Cannot read property ' resolve'of undefined'

标签 javascript runtime-error this

我不断收到错误“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/

相关文章:

Java - 转换 "this[int x, int y]"

javascript - 计算下一个纬度

javascript - 在另一个对象的 foreach 函数中访问对象的 "this"?

javascript - 为什么 "this"值不同?

c++ - 奇怪的访问冲突错误

c++ - 为什么我得到 : "cannot seek vector iterator after end"

Go 的垃圾收集器在使用时删除 ZeroMQ 套接字

javascript - 清除主干模型/集合内存泄漏

javascript - 旋转正方形的逻辑

javascript - Tic-Tac-Toe 游戏不会切换玩家、更新分数或更换玩家