我怎样才能解决可能未处理的承诺拒绝?
how can ı do solve Possible Unhandled Promise Rejection?
本机反应:0.61.2
react-native-fs: ^2.16.2
我想在外部存储中创建一个新目录。我预计 "Possible Unhandled Promise Rejection (id: 2): Error: Directory could not be created"
export function createDirCategory(fileName) {
const file = `${dirHome}/${fileName}`;
return new Promise((resolve, reject) => {
RNFS.mkdir(file)
.then(() => {
resolve(true);
})
.catch(error => {
reject(error);
});
});
};
addCategorty() {
const {categoryName} = this.state;
createDirCategory(categoryName);
const folderUrl =getDirUrl(categoryName);
let id =
Math.random()
.toString(36)
.substring(2) + Date.now().toString(36);
this.props.changeState;
store.push('categories', {
id,
categoryName,
imageUrl: [],
folderUrl,
});
this.setState({categoryName: ''});
}
您在使用 android 吗?如果您正在使用 android 那么您应该使用 PermissionsAndroid 来请求权限。
import {PermissionsAndroid} from 'react-native'
...
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Permissions for write access',
message: 'Give permission to your storage to write a file',
buttonPositive: 'ok',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the storage');
} else {
console.log('permission denied');
return;
}
} catch (err) {
console.warn(err);
return;
}
}
本机反应:0.61.2 react-native-fs: ^2.16.2
我想在外部存储中创建一个新目录。我预计 "Possible Unhandled Promise Rejection (id: 2): Error: Directory could not be created"
export function createDirCategory(fileName) {
const file = `${dirHome}/${fileName}`;
return new Promise((resolve, reject) => {
RNFS.mkdir(file)
.then(() => {
resolve(true);
})
.catch(error => {
reject(error);
});
});
};
addCategorty() {
const {categoryName} = this.state;
createDirCategory(categoryName);
const folderUrl =getDirUrl(categoryName);
let id =
Math.random()
.toString(36)
.substring(2) + Date.now().toString(36);
this.props.changeState;
store.push('categories', {
id,
categoryName,
imageUrl: [],
folderUrl,
});
this.setState({categoryName: ''});
}
您在使用 android 吗?如果您正在使用 android 那么您应该使用 PermissionsAndroid 来请求权限。
import {PermissionsAndroid} from 'react-native'
...
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: 'Permissions for write access',
message: 'Give permission to your storage to write a file',
buttonPositive: 'ok',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the storage');
} else {
console.log('permission denied');
return;
}
} catch (err) {
console.warn(err);
return;
}
}