'Unhandled promise rejection, [TypeError: undefined is not a function]
I have tried almost everything to fix this issue and cannot understand why this is still happening. I'm trying to generate a table and convert to an excel file and the code should be right I just don't understand why this isn't working. This is the file the error is in. If anyone has a clue to why this could be happening please help. The error is
Unhandled promise rejection, [TypeError: undefined is not a function]
import React from 'react';
// Required to save to cache
import * as FileSystem from 'expo-file-system';
// ExcelJS
import ExcelJS from 'exceljs';
// Share excel via share dialog
import * as Sharing from 'expo-sharing';
// From @types/node/buffer
import { Buffer as NodeBuffer } from 'buffer';
import {
StyleSheet,
Text,
View,
Image,
SafeAreaView,
FlatList,
ActivityIndicator,
TextInput,
TouchableHighlight,
Modal,
Animated,
Platform,
RefreshControl,
ScrollView,
Easing,
Button,
Dimensions,
StatusBar
} from "react-native";
import { Sizes, Colors, Padding, Margin, Fonts } from "app/styles"
import GriffinPoolsLogo from "images/griffin-logo.png"
import { addCustomer, getCustomers} from "app/classes/griffin_api.js"
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
import Header from 'views/shared/header.js'
import CustomerDetail from 'views/customer_detail.js'
import AddCustomer from 'views/add_customer.js'
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'red',
height:100,
width:100,
alignItems: 'center',
justifyContent: 'center',
},
});
// This returns a local uri that can be shared
const generateShareableExcel = async (): Promise<string> => {
const now = new Date();
const fileName = 'CustomerData.xlsx';
const fileUri = FileSystem.cacheDirectory + fileName;
return new Promise<string>((resolve, reject) => {
const workbook = new ExcelJS.Workbook();
workbook.creator = 'Me';
workbook.created = now;
workbook.modified = now;
// Add a sheet to work on
const worksheet = workbook.addWorksheet('My Sheet', {});
// Just some columns as used on ExcelJS Readme
worksheet.columns = [
{ header: 'Id', key: 'id', width: 10 },
{ header: 'Name', key: 'name', width: 32 },
{ header: 'D.O.B.', key: 'dob', width: 10, }
];
// Add some test data
worksheet.addRow({ id: 1, name: 'John Doe', dob: new Date(1970, 1, 1) });
worksheet.addRow({ id: 2, name: 'Jane Doe', dob: new Date(1969, 2, 3) });
// Test styling
// Style first row
worksheet.getRow(1).font = {
name: 'Comic Sans MS', family: 4, size: 16, underline: 'double', bold: true
};
// Style second column
worksheet.eachRow((row, rowNumber) => {
row.getCell(2).font = {
name: 'Arial Black',
color: { argb: 'FF00FF00' },
family: 2,
size: 14,
bold: true
};
});
// Write to file
workbook.xlsx.writeBuffer().then((buffer: ExcelJS.Buffer) => {
// Do this to use base64 encoding
const nodeBuffer = NodeBuffer.from(buffer);
const bufferStr = nodeBuffer.toString('base64');
FileSystem.writeAsStringAsync(fileUri, bufferStr, {
encoding: FileSystem.EncodingType.Base64
}).then(() => {
resolve(fileUri);
});
});
});
}
const shareExcel = async () => {
const shareableExcelUri: string = await generateShareableExcel();
console.log(shareableExcelUri, "URI")
Sharing.shareAsync(shareableExcelUri, {
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // Android
dialogTitle: 'Your dialog title here', // Android and Web
UTI: 'com.microsoft.excel.xlsx' // iOS
}).catch(error => {
console.error('Error', error);
}).then(() => {
console.log('Return from sharing dialog');
});
}
export default function GenerateExcel() {
console.log(generateShareableExcel, "fjkla")
return(
<View style={{width:"100%", height:180, marginTop:-Margin.xxlargeMargin * 1.5, backgroundColor:Colors.primaryLight}}>
<TouchableHighlight
style={{}}
onPress={shareExcel}
>
<Text style={{color:'white'}}>Generate Table</Text>
</TouchableHighlight>
</View>
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
