[英]Possible Unhandled Promise Rejection undefined is not a function
I am using React Native.我正在使用 React Native。 I have already check out What is an unhandled promise rejection?
我已经查看过什么是未处理的承诺拒绝? , but I can't understand it at all.
,但我完全看不懂。
When I create a component:当我创建一个组件时:
render(){
const MenuComponent = (
<MenuScreen CloseSlideMenu={this.SlideMenu.CloseSlideMenu} />
)
...
}
I get the following error:我收到以下错误:
'Possible Unhandled Promise Rejection (id: 0) TypeError: undefined is not a function (evaluating '_this.OpenSlideMenu.bind(true).then(function() {}))'
'可能未处理的承诺拒绝(ID:0)类型错误:未定义不是函数(评估'_this.OpenSlideMenu.bind(true)。then(function(){}))'
this.OpenSlideMenu()
is declared in the constructor()
. this.OpenSlideMenu()
在声明constructor()
constructor (props, context) {
super(props, context)
this.OpenSlideMenu = this.OpenSlideMenu.bind(true).catch(function(e){
console.log("Promise Rejected");
});
this.CloseSlideMenu = this.CloseSlideMenu.bind(true).catch(function(e){
console.log("Promise Rejected");
});
}
this.drawer is declared in the render() method: this.drawer 在 render() 方法中声明:
render () {
const MenuComponent = (
<MenuScreen CloseSlideMenu={this.SlideMenu.CloseSlideMenu} />
)
return (
<Drawer
ref={(ref) => this.drawer = ref}
content={MenuComponent}
tapToClose={true}
openDrawerOffset={170}
stles={drawerStyles}
panCloseMask={0.2}
closedDrawerOffset={-3}
styles={drawerStyles}
tweenHandler={(ratio) => ({
main: { opacity: (2-ratio)/2 }
})}>
<GroceryScreen selectedCategory='Todos' OpenSlideMenu={this.OpenSlideMenu} />
</Drawer>
)
}
Could someone explain to me why I have this error?有人可以向我解释为什么我有这个错误吗? How do I fix this?
我该如何解决?
Couple things wrong.夫妇的事情错了。 You're binding a boolean value as the
this
context of your function with .bind(true)
.您正在使用
.bind(true)
将布尔值绑定为函数的this
上下文。
You're also using .catch()
on the function declaration.您还在函数声明中使用了
.catch()
。 .then()
and .catch()
are used on the function invocations. .catch()
.then()
和.catch()
用于函数调用。
Also if this is the initial declaration of the function, you are trying to .bind()
to it before it is declared.此外,如果这是函数的初始声明,则您正在尝试在声明之前对其进行
.bind()
。 You would need to declare it first.您需要先声明它。
I recommend you read about .bind() and Promise over at MDN.我建议你在 MDN 上阅读.bind()和Promise 。
Here is a little example that hopefully will help you understand these principles:这里有一个小例子,希望能帮助你理解这些原则:
class Demo extends PureComponent {
constructor( props ) {
// allow the user this in constructor
super( props );
// set default state
this.state = {
text: "Awaiting data..."
}
// only needed if used in a callback
this.method = this.method.bind( this );
}
componentDidMount() {
// calling the method that returns a promise when component mounts
this.method()
.then((res) =>{
// set state in a non-mutating way
this.setState({text: res});
});
}
// declaring a method to handle the api call
// yours will not be named method
method() {
return new Promise(
(resolve, reject) => {
// simulating network delay of 2 seconds ( lets hope not! )
setTimeout(() => {
resolve( "Data returned from promise" );
}, 2000 );
});
);
}
render() {
// destructure the text var from data
const { text } = this.state;
// return text
return (
<p>{ text }</p>
);
}
};
I was getting a similar error for a different reason: importing an aync function "myFunc" from a Context file using useContext由于不同的原因,我遇到了类似的错误:使用 useContext 从上下文文件中导入 aync 函数“myFunc”
My error: Unhandled Promiose Rejection is not a function is an instance of a promise我的错误:未处理的承诺拒绝不是一个函数是一个承诺的实例
const {
state: { some, stuff }, myFunc,
} = useContext(SomeContext);
when calling myFunc which took no params/variables do not include parentheses.调用没有参数/变量的 myFunc 时不包括括号。 Changing
const output = await myFunc();
更改
const output = await myFunc();
to const output = await myFunc;
const output = await myFunc;
fixed it for me.为我修好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.