Hi, I am trying to create new records in my database inside my thunkable app.
The data will be coming from an API call. I have managed to get the data into my app via API but I am having trouble with what I think the community refers to as the “CORS error”. This means I have to keep using my phone, getting a token and entering in the computer to test every little thing I do. Therefore, I have used the method that I saw on a tutorial by “drted” to create the data structure in a variable until I am happy with how to handle the data. The example is a somewhat simplified structure from the one I want to use ultimately but I think for my questions it might be better to keep it as simple as possible.
In the blocks I have shown, I am able to get the “nombre” property from “1” which in this case is “dean” using the list element shown slotted. If i use the list that i have labelled B, I am able to get “alan,dean”.
I would like to create a record/row for each person in a loop and parse the nombre and age into the record.
I appreciate I haven’t created a loop yet and I will work on this next but before I continue, I would like some help with the approach to use.
My questions are:
- How far away from the solution am i?
- Should I collect the nombre and age together in each iteration of the loop or loop twice (once to get the nombres and a second time to get the ages)?
- If the data is collected, am I using the correct way to create a row? - i.e. by calling a seperate function?
If anyone could show me the block structure I need, it would be very much appreciated.
I have only been using Thunkable a few days and it has taken around 10 hours to get to where I am now.
I find this to be really confusing:
Are you naming the variables that way to save time or is there a reason for using hhh, hhhh, etc.? I also don’t understand why you’re using so many variables for a single list of objects. At most, you need two variables for that. And then converting an object to JSON only to then convert it back to an object…?
Yes, you’re close to a solution. I would not use a function (createRec) within a loop because the function call happens and then immediately, the loop cycles again. Instead, put the
create row block directly into the loop. If you need something to happen after creating each row, that’s what you would put into the “do” section of that block.
As for extracting two properties from an object, again, you don’t need so many variables. Just use a
count with i from 1 to number of rows in People loop and set the create row’s Nombre value to
get property of object "nombre" from list # i and set the Age value to
get property of object "age" from list # i .
If the rows don’t all get created, then you may need to slow down your loop to allow the
create row block time to complete. If that’s the case, try putting a short Wait block at the end of the loop or in the “do” section of the create row block. Start with 0.1 then 0.05 then 0 seconds. Sometimes a 0 second Wait block is enough of a delay – because that blocks still takes time to run!
Just a side note that the Tutorials and DIY Guides category is for people offering tutorials. I moved this to Questions about Thunkable which is the main category that people use for questions like yours.
Hi tatiang, I was really struggling to get the result I needed and, at one point I had all of that confusing set of variables displaying to labels so that I could see the outputs and try to work out what I needed. Most are redundant in the current state and I should have removed them before making post here, to avoid confusing people. Thank you so much for your reply, I will work through your answers and suggestions and post my my final blocks when it is all working. Hopefully this will benefit others. I will will remove the unrequired variables before my next post.
I am facing a problem with my API it keeps showing “null” and i have tried diff ways to fix it but didnt succeed does anyone have a way to solve it? this is my API https://api.carbonintensity.org.uk/intensity
Add a logic to if block “error or response = null”
This will avoid thr issue.
like this? still showing null
Revert your blocks back to what you had before with simple error checking.
Try using the property “data.from” instead of just “from”.
Hi Referring back to my original post and reply. The following seems to work for me for my objective of getting the nombre and age properties from an object in a list and creating a “record” row in my local Database. Per tatiang’s reply, it was a little messy and confusing. As promised, I have shown the blocks below for my current solution. This seems to work. However, if anyone would like to highlight anything they think I have done incorrectly, it would be much appreciated and hopefully beneficial to the community. Also, if others are in agreement that it works but would like to show any ways the blocks could be lessened to achieve the same effect, that would be equally beneficial.
- Is this the correct way to do it
- Please provide any improvements.
Here’s the data set in a variable - the names are different from the first post
And, here are the blocks that seem to be working
- One last question - I have a dataviewer on the same layout that updates to show 3 new records all with the last record (Casper 49). It does not show the 3 separate record datas. However, if I navigate to another layout (in this case screen3) where I have a dataviewer it shows the 3 new records. It is not a major issue but is there a way the get the data to show correctly in the DV on the original screen?
I feel confident the records are being created - just not shown immediately on the original DV. Maybe it is because I am test through my browswer and not the device?