React app builds locally without issue, but crashes on Heroku - reactjs

I've built a React / Express app, and it runs pretty well on my local machine. The API is all hooked up, and Heroku recognises all of that, but when Heroku tries to map an array of objects to React components it fails with the following, deeply unhelpful error:
TypeError: d is null
h NoteList.jsx:130
React 8
ai
Bi
$l
Ou
xu
Su
vu
Qa
unstable_runWithPriority scheduler.production.min.js:18
React 5
Va
Qa
$a
fu
Ni
e NoteList.jsx:21
s runtime.js:63
_invoke runtime.js:293
E runtime.js:118
Babel 2
I've tried several solutions but nothing seems to work.
The repo is here, the app is live on Heroku.
The troublesome piece of code seems to be in client/src/Components/NoteList.jsx. It fetches notes from the local storage via localforage like so:
const getLocalNotes = async () => {
try {
setLoading(true)
const notes = await localforage.getItem("notes");
setLoading(false);
setNoteList(notes);
} catch (error) {
console.error(error);
}
}
...which seems to throw an error later at line 130:
{ loading ? <p>Loading</p> :
<div id="note-list">
{noteList.map(note =>
<Note
key={note.id}
id={note.id}
text={note.text}
deleteNote={deleteNote}
editNote={editNote} />
)}
</div>
}
I'm quite new to all of this -- this app is the final project of a full stack developer course -- so apologies if I've missed something obvious.

You can add a && condition as your noteList is coming as undefined and then you are applying map() on it, resulting in error -
Error - Cannot read property 'map' of null
Fix -
{noteList && noteList.map(x => <your code>)}

Related

TypeError: (0 , import_jsx_dev_runtime.jsxDEV) is not a function

I am testing s remix.run app to see how it works.
I created a posts page inside routes directory
const Posts = () => {
return (
<main>
testing
</main>
);
};
I get this error on console:
TypeError: (0 , import_jsx_dev_runtime.jsxDEV) is not a function 2ms
at /home/Documents/remix/my-remix-app/app/entry.server.jsx:82:7
at new Promise (<anonymous>)
at handleBrowserRequest (/home/Documents/remix/my-remix-app/app/entry.server.jsx:78:10)
at handleRequest (/home/Documents/remix/my-remix-app/app/entry.server.jsx:23:7)
at handleDocumentRequestRR (/home/Documents/remix/my-remix-app/node_modules/#remix-run/server-runtime/dist/server.js:260:20)
at requestHandler (/home/Documents/remix/my-remix-app/node_modules/#remix-run/server-runtime/dist/server.js:49:18)
at /home/Documents/remix/my-remix-app/node_modules/#remix-run/express/dist/server.js:39:22
On browser I see
Unexpected Server Error
This is an active issue on the official remix repository. A few fixes suggested by users:
Adding remix build to dev script in package.json:
"dev": "remix build && run-p dev:*"
Forcefully bumping esbuild version to ^0.15.8.
Reverting #remix-run/dev to 1.6.8.

Next.js 13 - Fetch Failed Error. How do I resolve this?

I was trying the Next 13 beta version, and I faced a strange problem. What I am trying to do is, fetch data on the server side and display them on the page. However, the "fetch" operation fails on the server side. Below is the code for the Next.js page. It falls under the 'app' directory, as 'app/pageName/page.js'
import React from 'react'
async function callApi() {
const data = await fetch('https://marketplace-api.scistoneprojects.com/api/items/?search=yil');
return data.json();
}
export default async function Page() {
const data = await callApi();
return (
<main>
{data.results && data.results.map((product, index) => (
<h1>{product.title}</h1>
))}
</main>
)
}
Click to see Error Message. (UND_ERR_CONNECT_TIMEOUT)
Click to see API response (Django REST)
Click to see Next 13 Doc
Note: The fetch operation fails after ~ 10 seconds.
What I did:
I tried Axios, but it also fails.
I tried adding 'enableUndici: true' to the next config file. (Fails)
I tried other mock APIs, some work some don't. (Weird)
They all work normally on the client side.
They all work normally in the Next 12.
They all work normally on any other React app.
Versions:
node 18.12.0
next 13.1.0
react 18.2.0
react-dom 18.2.0
npm 9.2.0
Machine: Mac Mini M1 (Ventura 13.1)

Nextjs 12 Error When Running on Cloud Run But Works Well on Localhost

I'm trying to upgrade from nextjs v10 to v12. It successfully worked at localhost, but when deployed on cloud run and running on the active domain, it shows 500 Internal Server Error. Previously it worked well on cloud run with nextjs v10.
I create a test page to check what makes it error and after testing, it may seems the 'getServerSideProps' is the problem.
Here is my code, /pages/test2.js
import Layout from 'components/layout'
export async function getServerSideProps({ req, res }) {
return {
props: {
data: { title: 'test' },
},
}
}
export default function Test({ data }) {
console.log('testtt', data)
return (
<Layout activeId="home">
<div>ABC TEsT</div>
</Layout>
)
}
If I remove 'getServerSideProps' function then deploy to cloud run, then the page is working well.
Here is the error found on cloud run server log
SyntaxError : Unexpected token '.'
Link to error image
The weird thing is, there is no error on localhost.
Is there any problem with getServerSideProps on nextjs v12 or I am missing configuration?
I have no custom webpack and babel config.
Thank you

Firebase production Error: ReferenceError: emptyChildrenSingleton is not defined

I have a nextjs project, v12.1.3 with react v18.1.0. I use firebase realtime database to handle notifications into my project. It works in develop mode, but it doesn't in production (on built environment).
The errors I get are the following:
ReferenceError: emptyChildrenSingleton is not defined
at new bV (a198fdd9-b0625f5d1b77c03a.js:1:75705)
at new cF (a198fdd9-b0625f5d1b77c03a.js:1:95593)
at a198fdd9-b0625f5d1b77c03a.js:1:124619
at get _repo [as _repo] (a198fdd9-b0625f5d1b77c03a.js:1:124994)
at get _root [as _root] (a198fdd9-b0625f5d1b77c03a.js:1:125199)
at d_ (a198fdd9-b0625f5d1b77c03a.js:1:120499)
at n (index-493c2cb0fc06fe87.js:1:879)
at index-493c2cb0fc06fe87.js:1:826
at g0 (framework-47484f2290a3befd.js:1:91915)
at h9 (framework-47484f2290a3befd.js:1:113308)
[2022-06-13T21:12:40.922Z] #firebase/database: FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read properties of undefined (reading 'syncPointTree_')
at cM (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:99754)
at cI (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:97044)
at dC (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:109983)
at http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:124197
at ap (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:3775)
at d.onServerInfoUpdate_ (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:124182)
at d.handleTimestamp_ (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:43294)
at d.onReady_ (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:39829)
at aM.onConnectionEstablished_ (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:26211)
at aM.onHandshake_ (http://localhost:3000/_next/static/chunks/a198fdd9-b0625f5d1b77c03a.js:1:25266)
Uncaught TypeError: Cannot read properties of undefined (reading 'syncPointTree_')
at cM (a198fdd9-b0625f5d1b77c03a.js:1:99754)
at cI (a198fdd9-b0625f5d1b77c03a.js:1:97044)
at dC (a198fdd9-b0625f5d1b77c03a.js:1:109983)
at a198fdd9-b0625f5d1b77c03a.js:1:124197
at ap (a198fdd9-b0625f5d1b77c03a.js:1:3775)
at d.onServerInfoUpdate_ (a198fdd9-b0625f5d1b77c03a.js:1:124182)
at d.handleTimestamp_ (a198fdd9-b0625f5d1b77c03a.js:1:43294)
at d.onReady_ (a198fdd9-b0625f5d1b77c03a.js:1:39829)
at aM.onConnectionEstablished_ (a198fdd9-b0625f5d1b77c03a.js:1:26211)
at aM.onHandshake_ (a198fdd9-b0625f5d1b77c03a.js:1:25266)
The integration is very basic: I write my notification client side and create them server side with firebase-admin. So the problem is reading from realtime database (in prod only).
The code is something like this (i simplified it)
const [textData, setTextData] = useState<any[]>([]);
useEffect(() => {
initFirebase();
fetchData();
}, []);
const fetchData = () => {
const db = getDatabase();
const starCountRef = ref(db, 'testData');
onValue(starCountRef, (snapshot) => {
const data = snapshot.val();
setTextData(
Object.entries(data || {}).map(
([key, value]: [key: string, value: any]) => ({
key,
text: value?.text,
})
)
);
});
};
Where initFirebase() is a classic:
import { getApps, initializeApp } from 'firebase/app';
const firebaseConfig = {
//my configs
};
// Initialize Firebase
const initFirebase = () => {
if (getApps().length && getApps.length > 0) return getApps()[0];
return initializeApp(firebaseConfig);
};
export default initFirebase;
It might seem all right. And it is. I made this into an empty project and it actually work. (here the repo without the error).
I tried to replicate the error I have in my repo and I did it. This is the repo with the error. The error must be something with the packages, react or nextjs version, but I don't get it.
For simplicity You can take a look directly to the repos. I think the code is all right.
How do you think this error can be fixed?
P.S. the repos are also deployed on vercel, so you can try also to see the logs. Of course you can also make a PR.
I found out the problem is the last version of next (12.1.3).
In particular swcMinify. In fact, I had this option set to true into the file next.config.js. I also found out that there are several issues about swcMinify in production build. So, in order to solve the problem I described above, you just have to turn off swcMinify, or remove it. After that everything seems to be all right.
Conclusion: This is not a firebase error.

firebase serve - React Context returns undefined

I'm currently facing a problem with a firebase deployment, happening only in production environment. I cannot reproduce it in local, with the firebase serve command; even if I'm using the target 6.11.5 node version.
Here is my trouble : when deployed, the site will crash (server-side) rendering a React component using a Context's return property
Component extract :
<div className='rank-item col-sm-6 col-lg-3'>
<LangContext.Consumer>
{
({ lang, langs }) => (
// here, props are cools in development
// but are undefined in production
)
}
</LangContext.Consumer>
</div>
App root component (at this point, props.lang & props.langs are correctly set, even on production environment) :
export default props => (
<LangContext.Provider value={ {
lang: props.lang,
langs: props.langs
} }>
</LangContext.Provider>
)
The problem as shown in the console :
As you may have guess, I'm building my SSR code with webpack to commonjs2 module. The error stack trace here refers to this exact line of built code :
I'm a bit surprised because I already used React's Context feature on a production environment, but cannot figure now what could be the problem. Thanks for your help

Resources