enuchi / React-Google-Apps-Script

This is your boilerplate project for developing React apps inside Google Sheets, Docs, Forms and Slides projects. It's perfect for personal projects and for publishing complex add-ons in the Google Workspace Marketplace.
MIT License
1.32k stars 171 forks source link

Cannot read properties of undefined when trying to use a server function #194

Closed axeligl closed 1 year ago

axeligl commented 1 year ago

Hi! I'm having this error and I don't know if I'm doing something wrong or what.

I declared a server function in sheets.js, exported it in index.ts, used it on a component on client side and having a 'Cannot read properties of undefined ' error.

I make a simple example and still doesn't work.

This is index.ts

import {
  doGet
} from './ui';

import { getInfo } from './sheets';

// Public functions must be exported as named exports
export {
  doGet,
  getInfo,
};

This is sheets.js

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const getSheet = (name) => SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(name));

export const getInfo = () => {
  return "prueba"
}

And this is the component

import React, { useState, useEffect } from 'react';
import serverFunctions from '../../utils/serverFunctions';

export default function MesitasApp() {
 serverFunctions.getInfo().then(console.log).catch(alert)
  return (
    <div className="App">
    probando
</div>
 );
 }

When I deploy and run the app, there's an error on the console: TypeError: Cannot read properties of undefined (reading 'getInfo')

I used the previous version of this repo without any problem. Probably I'm doing something wrong but I'm stucked and wish to solve this problem soon. Thanks.

enuchi commented 1 year ago

You using any additional libraries? There was an issue with react router do in case you're trying that.

axeligl commented 1 year ago

No. Its a plain clone of this repo

enuchi commented 1 year ago

Does a brand new clone, with no modifications, work for you?

enuchi commented 1 year ago

Maybe see discussion at #183

axeligl commented 1 year ago

I tried the project without modifications and it works. Also I read that discussion but nothing works. I have this warning when deploy:

export 'default' (imported as 'serverFunctions') was not found in '../../utils/serverFunctions' (possible exports: __esModule, serverFunctions)

Seems to be related to the problem.

enuchi commented 1 year ago

Looks like you may be using default import instead of named import.

Try: import { serverFunctions } from '../../utils/serverFunctions';

axeligl commented 1 year ago

Yes. I realized that on your code was different. Just tried that and it's solved. I came here to comment. Thanks!