Misunderstanding of errors (JavaScript beginner)

Hello,

I’m trying to make a pack that generates random content to populate the tables, but I’m running into errors :).

I don’t know much about JavaScript and I don’t understand the errors shown.

If any of you could help me, that would be cool.

Sincerely,
Thierry

P.S: Here are the errors given =>

We found some errors: Failed Typescript validation.
On line 1 Cannot find name ‘pack’.
On line 5 Cannot find name ‘coda’.
On line 6 Cannot find name ‘coda’.
On line 12 Cannot find name ‘coda’.
On line 10 Property ‘Table’ does not exist on type ‘typeof ParameterType’.
On line 21 Property ‘Table’ does not exist on type ‘typeof ValueType’.
On line 43 Property ‘random’ does not exist on type ‘typeof import(“@codahq/packs-sdk”)’.
On line 46 Property ‘random’ does not exist on type ‘typeof import(“@codahq/packs-sdk”)’.
On line 48 Property ‘Date’ does not exist on type ‘typeof ValueType’.
On line 49 Property ‘random’ does not exist on type ‘typeof import(“@codahq/packs-sdk”)’.
On line 52 Property ‘random’ does not exist on type ‘typeof import(“@codahq/packs-sdk”)’.
On line 65 Property ‘createTable’ does not exist on type ‘typeof import(“@codahq/packs-sdk”)’.

Translated with DeepL Translate: The world's most accurate translator (free version)

import * as coda from "@codahq/packs-sdk";

export const pack = coda.newPack();

pack.addFormula({
  name: "Random Data Generator",
  description: "Generates random data for a given table.",
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.Table,
      name: "GeneratorDatas",
      description: "The table to generate random data for.",
    }),
    coda.makeParameter({
      type: coda.ParameterType.Number,
      name: "numRows",
      description: "The number of rows of random data to generate.",
      default: 10,
    }),
  ],
  resultType: coda.ValueType.Table,
  execute: async function ([table, numRows]) {
    // First, we get the column names and types of the input table
    const columnNames = table.columns.map((c) => c.name);
    const columnTypes = table.columns.map((c) => c.type);

    // Next, we create an empty array to store the rows of random data
    const rows = [];

    // Then, we loop through the number of rows specified by the user
    for (let i = 0; i < numRows; i++) {
      // For each row, we create an empty object to store the cell values
      const row = {};

      // We then loop through the column names and types to generate random data for each cell
      for (let j = 0; j < columnNames.length; j++) {
        const columnName = columnNames[j];
        const columnType = columnTypes[j];

        // Depending on the column type, we generate the appropriate type of random data
        switch (columnType) {
          case coda.ValueType.String:
            row[columnName] = coda.random.string();
            break;
          case coda.ValueType.Number:
            row[columnName] = coda.random.number();
            break;
          case coda.ValueType.Date:
            row[columnName] = coda.random.date();
            break;
          case coda.ValueType.Boolean:
            row[columnName] = coda.random.boolean();
            break;
          default:
            row[columnName] = null;
            break;
        }
      }

      // Finally, we add the row of random data to our array of rows
      rows.push(row);
    }

    // Once all the rows have been generated, we create a new table with the same column names and types as the input table, and return it with the rows of random data
    return coda.createTable(columnNames, columnTypes, rows);
  },
});

1 Like

Are you trying to run it on your local machine?
I think it’s easier to use the online editor if you are new.

Otherwise if you wanna get it working on your local machine, follow this guide, it’s really good!

Hi @Thierryvm, very cool idea for a Pack! I’m not sure about the errors related to pack and coda on lines 1, 5, 6, and 12. For the other errors, I think you are trying to use Coda actions and formulas inside your Pack code, but Pack code doesn’t support Coda formulas at the moment. At a high level, it looks like you want the Pack to create a new table, but currently there’s no way to create tables from Packs. The main things you can create with Packs are formulas and sync tables. You could probably create a sync table full of random data and the user could input how many rows of random data they want before running the sync.

2 Likes

Hello @Rickard_Abraham,

I use it with the online editor only.

Sincerely,
Thierry

Hi @Al_Chen_Coda,

Hello AI_Chen, thank you already for your support. Here, my goal is just to be able to populate a data table according to its type. This should make it possible to create templates quickly without having to add them yourself and thus save a lot of time :).

Secondly, indeed, my goal is to be able to design a complete database, but my experience is still far too weak in JavaScript. :frowning:

And by the way, if you want to get the code and improve it to help the community as soon as possible, you have a starting base ^^.

Personally, I only do this for the pleasure of helping.

Merry Christmas to all :slight_smile:

Sincerely,
Thierry

1 Like

Hi @Thierryvm - Excited to hear you are working on a Pack! While @Al_Chen_Coda has already provided a lot of good advice, one more tip is that if you see an error like Cannot find name ‘coda’. then it usually means something fundamental is going wrong with importing the SDK. The code you pasted seems to have a correct import of the SDK, but occasionally the Pack Studio web editor can have a temporary error loading the it. In those cases refreshing the page should resolve the issue.

1 Like

Hello @Eric_Koleda

Thank you for your precious advice :), I already did this and even changed my browser, but unfortunately this error persists :(.
For the others, I haven’t found a solution yet, because my experience in JavaScript is self-taught.

Best regards,

Thierry

Very odd indeed! Maybe it’s worth creating a new Pack and pasting in that code? Or perhaps creating a new Pack using one of the templates, and then slowly add in your code and see when it breaks.

1 Like

Hello @Eric_Koleda

Thank you for your precious advices :), I already did this and even used one of the template packs to see if I would have forgotten some information with the SDK, but nothing on this side :(.

However, it’s a good idea to proceed step by step and I’ll keep you informed of my tests :), hoping to get there one day hehe

Best regards,

Thierry

Bonjour @Eric_Koleda ,

Je dois ajouter ( import * as mathjs from “mathjs”; ). Cependant, il me le met en
image.

Je n’ai pas du voir l’information sur comment ajouter des dépendances :frowning:

Cordialement,
Thierryvm

I think I found the solution :(.

I’ll have to go through a local CLI environment to add the necessary libraries to improve my package ^^

Sorry, I didn’t see this post.

1 Like

Hello to you @Eric_Koleda , @Al_Chen_Coda ,

I think my mistakes are due to the fact that I try to do it in the studio pack and not with VS Code.

By, even following the method given in the help, I can’t get the right structure :frowning:

I would like to know @Eric_Noël, do you plan to make a complete video tutorial for beginners. I followed the webinar with @Thomas_Baucom and without success :frowning:

Sincerely,

Thierry

Ya, getting the CLI setup can be quite a journey! Do you know where you are stuck? Maybe the best way forward is to book some time during my office hours where we can dive in together.

2 Likes

Thank you very much @Eric_Koleda

Alas, I don’t speak English to hold a conversation :frowning:

Thanks anyway for the kind offer.

Sincerely,

Thierry

1 Like

No worries! Always happy to help you here if you can send along some screenshots or code snippets.

Regarding tutorials, I’m investigating creating some additional ones for the CLI, so stay tuned!

1 Like

Thank you for your answer

Here is what my workspace looks like, but nothing seems to work, yet I follow all the steps using the copy/paste provided ^^

Sincerely,
Thierry

Capture d’écran 2023-01-10 172742

Hello @Eric_Koleda

I really enjoy learning code since I started with Python, etc.
A question I have here, we use in the studio pack JavaScript, but do you think you will switch to TypeScript soon, because it is simpler and avoids typing errors quite a bit for example :).

And could you tell me where I can find the beta formulas ^^, see if I can find temporary solutions like that?

Sincerely,
Thierry

French speaking Youtubeur Coda : @codafrench (For those who want to understand the basics and more :slight_smile: )

Hello,

I followed the tutorial provided CLI to the letter, but I am on Windows and I read a post on GitHub that the environment would switch to nvm?

I created my pack.ts with the Node_modules folder, I have the API key, etc.
But, it generates me a lot of errors by testing :). And here, it tells me that my pack.ts file is not in the right directory. But it is in the folder created at the beginning of the tutorial and as a proof, it worked with the test one.
Thank you for your understanding.

Sincerely,
Thierry

image

image

The Pack Studio actually uses TypeScript already, although without some of the strict features turn on so that you can write more like plain JavaScript if you like.

What you showed in your screenshot is just warnings, feel free to ignore those.

Getting the SDK to work locally is more challenging in windows for sure. I just drafted an alternate version of the tutorial that works in GitHub Codespaces, an online environment that is easier to get setup correctly. If you want to try out that draft tutorial, you can find it here:

https://coda-packs-sdk.readthedocs-hosted.com/en/latest/tutorials/get-started/github/

1 Like

Hello @Eric_Noël,

I finally managed to put my pack online last night, well at night hehe. So, if I write all the code in TypeScript in the pack.ts file, do I need to do a tsc to convert it to JS or not ?

The problem when you’re not a pro :), is that the ide offers a lot of options and so, you end up with files like “.hintrc”, etc.

I know a bit about CodeSpace and I will use your solution and see the difference. Hopefully I can use formulas to be able to change the colors. Since my package should allow changing the background colors of the header of a table according to a name or a list or even why not a color wheel.

Sincerely,

Thierryvm

P.S: GitHub - coreybutler/nvm-windows: A node.js version management utility for Windows. Ironically written in Go. i have find this too :slight_smile: