Hi everyone,
I have massive JavaScript array which contains the menu for a cafe. When I found out about the Square APIs, I decided to store my menu in Square’s database instead. To do this, rather than manually copy over every single menu item, mod, etc. I decided to try using the batchUpsertCatalogObjects. and some iterations to automate the process.
Here’s the JavaScript code that was meant to upload the entire menu to the Square database:
var objects = []
for( let category of menu ){
// adds the category itself
objects.push({
type:'CATEGORY',
categoryData:{
name:category.categoryName
},
id: '#' + category.categoryName
})
// adds each of its category items
for( let item of category.categoryItems ){
// first the array itself is defined
var modifiers = [];
if( item.mods ){
// then the mods are processed and added to the array
for( let mod of item.mods ){
modifiers.push({
type:'MODIFIER',
id:'#' + mod.name,
modifierData:{
name: mod.name,
modifier_list_id: '#' + item.name + '_modifierList'
}
})
}
}
// then the modifier list is added to the objects array
objects.push({
type:'MODIFIER_LIST',
id: '#' + item.name + '_modifierList',
modifierListData:{ modifiers: modifiers }
})
// then the item is added to the array and linked to the modifierlist by the categoryId
objects.push({
type:'ITEM',
id:'#'+ item.name,
itemData:{
name: item.name,
categoryId: '#' + category.categoryName
},
modifierListInfo:{
modifierListId: '#' + item.name + '_modifierList'
}
})
}
}
var BatchUpsertCatalogObjectsRequest = {
idempotencyKey:'a8caff16-7267-47a9-8423-2317c00b08e6', // uuid generated on the internet
batches:[{ objects: objects }]
}
catalogApi.batchUpsertCatalogObjects( BatchUpsertCatalogObjectsRequest ).then(( response )=> console.log( response ))
And here’s the error I get when I run it over my menu
array:
errors: [
[Object: null prototype] {
category: 'INVALID_REQUEST_ERROR',
code: 'INVALID_VALUE',
detail: ''
}
]
Does anyone know why this might be happening? Thanks in advance.