//get table info
let table = base.getTable("A2_複製");
let queryResult = await table.selectRecordsAsync();
let record;
let stocks = new Array(2);
//I wanna set to arrayri]y0~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.recordsoi];
stocksoi]s0].push(record.id);
stocksoi]s1].push(record.getCellValue("*印刷型番"));
} ;
console.log(stocks);
Page 1 / 1
Hello @111257, welcome to the community . I’m not sure what the script is trying to do but there are some semantic errors. E.g. stockski] 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 o0] 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.
Hello @111257, welcome to the community . I’m not sure what the script is trying to do but there are some semantic errors. E.g. stockski] 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 o0] 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.
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”.
I upload CSV(latest stack amount) from another system.
this is CSV.↓
If “item id” is much then update amount.
so,I first tried to set the CSV to an array.
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')
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
Looking at this now, there’s got to be a simpler way lol.
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')
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
Looking at this now, there’s got to be a simpler way lol.
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(latestStocks0i].Stock != 0){
for (let j = 0; j < merchandise.length; j++){
if(latestStocksri].ItemID === merchandise0j].name){
//console.log("much");
await merchandiseTable.updateRecordAsync(merchandisecj].id, {
"amount" : latestStocksei].Stock,
})
//muchCount++
}else{
}
}
}
}
//console.log(muchCount)
but I have to upgrade this script so it is so slowly. I’ll work hard to do!