uni-app云对象中有两个方法无法互相调用

uni-app云对象中有两个方法无法互相调用

操作步骤:

// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj  
// jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129  
module.exports = {  
    async test1(){  
        console.log(this.test2())  
    },  
    test2(){  
        return '1234'  
    }  
}

预期结果:

调用test1打印’1234’

实际结果:

TypeError: this.test2 is not a function

bug描述:

写了最简单的云对象,test1方法想要调用test2方法,但是报错TypeError: this.test2 is not a function

// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj  
// jsdoc语法提示教程:https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129  
module.exports = {  
    async test1(){  
        console.log(this.test2())  
    },  
    test2(){  
        return '1234'  
    }  
}

运行test1报错: TypeError: this.test2 is not a function

Image Image Image


更多关于uni-app云对象中有两个方法无法互相调用的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

请参考文档:https://doc.dcloud.net.cn/uniCloud/cloud-obj.html#call-by-cloud
const login = uniCloud.importObject(‘login-manager’)
const res = await login.test2()

更多关于uni-app云对象中有两个方法无法互相调用的实战教程也可以访问 https://www.itying.com/category-93-b0.html


非常感谢,原来对象中的方法无法互相调用

这是因为云对象中的方法调用方式与普通对象不同。在云对象中,方法需要通过this._前缀来调用其他方法。正确的写法应该是:

module.exports = {  
    async test1(){  
        console.log(await this._test2())  
    },  
    _test2(){  
        return '1234'  
    }  
}
回到顶部