question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js app with DataStore works locally but not when deployed to Amplify Hosting

See original GitHub issue

Before opening, please confirm:

  • I have checked to see if my question is addressed in the FAQ.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.

App Id

d8dd1y47jar7i

AWS Region

us-west-2

Amplify Hosting feature

Not Applicable

Describe the bug

I’ve created a simple Next.js app that uses a DataStore.query to fetch some data I generated in Amplify Studio.

It works locally but when deployed to Amplify Hosting it throws an exception before posting the query.

Here’s the exception:

TypeError: Cannot read properties of undefined (reading ‘length’) at n.copyOf (zen-observable.js:444:2) at uq (zen-observable.js:444:2) at e.<anonymous> (zen-observable.js:444:2) at zen-observable.js:444:2 at Object.next (zen-observable.js:444:2) at s (zen-observable.js:444:2)

Expected behavior

Here’s how the page renders locally:

image

Reproduction steps

Here’s my repo: https://github.com/tombray/amplify-test

The DataStore.query is in pages/index.js:

export default function Home() {
  const [stations, setStations] = useState([]);

  useEffect(() => {
    const getStations = async () => {
      console.log("before datastore query");
      try {
        const stations = await DataStore.query(Stations);
        console.log(stations);
        setStations(stations);
      } catch (e) {
        console.log(e);
      }
    };

    getStations();
  }, []);

Running that locally works for me but when I deploy it with Amplify Hosting, it throws an exception which you can see here: https://main.d8dd1y47jar7i.amplifyapp.com/

Build Settings

No response

Log output

# Put your logs below this line


Additional information

No response

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tombraycommented, Dec 5, 2022

Aha! Thanks @svidgen !!

1reaction
svidgencommented, Dec 5, 2022

That could easily be DataStore nuance. Remember that DataStore syncs in the background. The full dataset isn’t necessarily available at the time you query().

Take a look at either:

  1. Waiting for the ready event to ensure the sync is finished first.
  2. observeQuery(), to populate and refresh data on the page as it syncs.

Hope that helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js app with DataStore works locally but not when ... - GitHub
I've created a simple Next.js app that uses a DataStore.query to fetch some data I generated in Amplify Studio. It works locally but...
Read more >
Host a Next.js SSR app with real-time data on AWS Amplify
In this blog post we are going to deploy a Next.js app on the AWS Amplify console, set up a custom domain, enable...
Read more >
Hosting - Next.js - JavaScript - AWS Amplify Docs
In this guide you'll learn how to deploy a Next. js app using Amplify Hosting. Amplify supports the hosting of static apps and...
Read more >
Next.js with AWS Amplify Admin UI Crash Course - YouTube
In this video you'll learn how to build a full stack Next. js app on AWS in less than 25 minutes. You'll learn...
Read more >
AWS Amplify: A review of advanced features - LogRocket Blog
Using the CLI and library toolchain provided by Amplify, you can quickly build, deploy, host, experiment and iterate web and mobile apps at ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found