Help

Re: I can't set to Array

Solved
Jump to Solution
3113 0
cancel
Showing results for 
Search instead for 
Did you mean: 
111257
6 - Interface Innovator
6 - Interface Innovator

Hello. Nice to mee you.
I have this probrem.Preformatted text
please let me know.

//get table info
let table = base.getTable("A2_複製");
let queryResult = await table.selectRecordsAsync();

let record;
let stocks = new Array(2);

//I wanna set to array[i][0~1], but I can't... Error tell me  "TypeError: Cannot read property '0' of undefined"

for(let i = 0; i < queryResult.records.length; i++){
    record = queryResult.records[i];
    stocks[i][0].push(record.id);
    stocks[i][1].push(record.getCellValue("*印刷型番"));
} ;

console.log(stocks);

image

1 Solution

Accepted Solutions
Dominik_Bosnjak
10 - Mercury
10 - Mercury

Just to be clear, you want these columns to match, regardless of whether the difference is big or small, yeah? If that’s the case, your first instinct was pretty good. I don’t work much with CSVs but they’re basically plaintext so if you have them in the table already, you should be able to do something like this:

let 
    q = await base.getTable(cursor
        .activeTableId)
        .selectRecordsAsync(), // pretend this is your original base
    array = []

q.records.forEach( record => { 
    
    let 
        itemId = record.getCellValue('item id'), 
        amount = record.getCellValue('amount')

        itemId !== amount
                ? array.push(
                    {id:record.id,
                        fields:{'amount':itemId}}
                        ) 
                : console.log(`Record ${record.name} doesn't need updating.`)



I tried using your actual field/table names and from what I’ve gathered, you actually have these imports in the same table as the “correct” values. Even if not, I think you’ll be able to figure it out from here anyway; like I said, your instinct was good but instinct doesn’t get you far with ecmascript haha.

What you were trying to do (I think) was a bit more complicated, but also doable. Unintentional mutations are a big no-no on the web, however, which is one of the reasons your syntax didn’t work - it’s meant to be counterintuitive so that you don’t accidentally change your Airtable records, in this instance.

Just for future reference, though, this is the easiest way that currently comes to mind for how to both create and fill an array with an arbitrary number of… numbers:

const range = (a, z) => Array.apply(0, Array(--z)).map((e, i) => i+ a)

Whereby a is your desired starting number and z your exclusive cap. So, range(1,10) would give you this output
image

Looking at this now, there’s got to be a simpler way lol.

See Solution in Thread

5 Replies 5
Raminder_Singh
7 - App Architect
7 - App Architect

Hello @111257, welcome to the community :slightly_smiling_face: . I’m not sure what the script is trying to do but there are some semantic errors. E.g. stocks[i] will return the ith item which will be undefined since it doesn’t contain any objects after the let stocks = new Array(2); statement (there are two slots for objects in the array, but those slots are not filled with any object). Then the next [0] is trying to get the 0th item from undefined. Hence the error - Cannot read property '0' of undefined.

If you could maybe describe what the script is trying to do we can help you.

new Array(2) just leaves you with an empty array whose length is set at 2, any index number will return undefined.

Please try explaining what you’re trying to accomplish in a bit more detail as I’m afraid this code isn’t too revealing.

111257
6 - Interface Innovator
6 - Interface Innovator

Thank you so much, Raminder_Singh and Dominik_Bosnjak.

I work in Internetshop.

I have to update the stock amount of merchandise. The stock amount in Airtable’s table “C_商品” at Field “amount”. At that time Key is “item id”.
abe1c7f2f4064b62234e31d3845883508e004699.png

I upload CSV(latest stack amount) from another system.

this is CSV.↓
867115c14c149b69faac648e661d38c94bfcaf35.png

If “item id” is much then update amount.

so,I first tried to set the CSV to an array.
image

Dominik_Bosnjak
10 - Mercury
10 - Mercury

Just to be clear, you want these columns to match, regardless of whether the difference is big or small, yeah? If that’s the case, your first instinct was pretty good. I don’t work much with CSVs but they’re basically plaintext so if you have them in the table already, you should be able to do something like this:

let 
    q = await base.getTable(cursor
        .activeTableId)
        .selectRecordsAsync(), // pretend this is your original base
    array = []

q.records.forEach( record => { 
    
    let 
        itemId = record.getCellValue('item id'), 
        amount = record.getCellValue('amount')

        itemId !== amount
                ? array.push(
                    {id:record.id,
                        fields:{'amount':itemId}}
                        ) 
                : console.log(`Record ${record.name} doesn't need updating.`)



I tried using your actual field/table names and from what I’ve gathered, you actually have these imports in the same table as the “correct” values. Even if not, I think you’ll be able to figure it out from here anyway; like I said, your instinct was good but instinct doesn’t get you far with ecmascript haha.

What you were trying to do (I think) was a bit more complicated, but also doable. Unintentional mutations are a big no-no on the web, however, which is one of the reasons your syntax didn’t work - it’s meant to be counterintuitive so that you don’t accidentally change your Airtable records, in this instance.

Just for future reference, though, this is the easiest way that currently comes to mind for how to both create and fill an array with an arbitrary number of… numbers:

const range = (a, z) => Array.apply(0, Array(--z)).map((e, i) => i+ a)

Whereby a is your desired starting number and z your exclusive cap. So, range(1,10) would give you this output
image

Looking at this now, there’s got to be a simpler way lol.

Thank you for your advaice!!

Yes! That’s right!

It’s not. bur Your scritp gave me a hint;)

I’ll studÿ Standard Javascript!!

Thanks to you I reach a goal at this way.

//CSVにて、最新在庫情報の取り込み
let latestStock = await input.fileAsync('Please CSV',
    {
        allowedFileTypes: ['.csv'],
        hasHeaderRow: true
    }
);

let latestStocks = latestStock.parsedContents;
//console.log(latestStocks);

//商品テーブルの取り込み
let merchandiseTable = base.getTable("C_商品");
let merchandises = await merchandiseTable.selectRecordsAsync();
let merchandise = merchandises.records;
//console.log(merchandise);

//let muchCount = 0;

for (let i = 0; i < latestStocks.length; i++){
        if(latestStocks[i].Stock != 0){
        for (let j = 0; j < merchandise.length; j++){
            if(latestStocks[i].ItemID === merchandise[j].name){
                //console.log("much");
                await merchandiseTable.updateRecordAsync(merchandise[j].id, {
                    "amount" : latestStocks[i].Stock,
                })
                //muchCount++
            }else{
            }
        }
    }
}
//console.log(muchCount)

but I have to upgrade this script so it is so slowly. I’ll work hard to do!