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.

[question] Using objects as template placeholders

See original GitHub issue

Hi,

I was trying out preval with NextJS to inline my data during build time.

This is my code:

static async getInitialProps() {
    const { data } = await Axios.get(
      "http://localhost:3100/graphql?query={ articles{ name slug } }"
    );
// data.data is an object
    const actualData = preval`
    const allArticles = ${data.data}
    module.exports = allArticles
    `;
    console.log(actualData);
    return { data };
  }

When I console log it, it returns an empty object. Am I doing something wrong here?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Feb 13, 2019

Ah, that’s tricky. What would probably be best is to have a script that loads the data into a cache before the build so you can get it synchronously during the build.

But if that doesn’t work, your code can do spawn.sync to run an asynchronous script synchronously. But that would make your build pretty slow.

1reaction
garnerpcommented, Feb 13, 2019

Hi @kentcdodds .

The use case @rtm619 is trying to achieve is to inline the data when doing a next.js export, that way when the site is served from the exported files that the getInitialProps function is returning the inlined content instead of going to the API. I think the problem is that preval only supports sync and the call here is async. Any ideas how to potentially do this using preval?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chapter 19. Templates: filling placeholders with data
This chapter shows you how to untangle the HTML from the data and the JavaScript by using templates. The designers on your team...
Read more >
C++ making a template class as place holder in another class
For this you first need a way to represet a C++ type (not object) into an ... And post another question better suited...
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick ... The strings and placeholders get passed to a function — either a default function ...
Read more >
Template Method Design Pattern - SourceMaking
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Base class declares algorithm 'placeholders', ...
Read more >
Problem adding objects to powerpoint slides when using ...
Hi, I'm having this problem. I'm working on a template to build automatic reports. The idea is to read data from an excel...
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