Basically. i’m trying to get JSON from a website, but when the website loads, not all of the data is retrieved from the website in time and therefore only some of it gets inserted into the collection. How can I allow all of the JSON to be read from the external website on load of the website. Thanks
import wixData from ‘wix-data’;
$w.onReady( function () {
fetch(‘https://jsonplaceholder.typicode.com/posts/’)
.then(response => response.json())
.then(json => {
var data = json
data = JSON.stringify(data);
data = data.split(“}”)
data[0] = “,” + data[0].substring(1)
for ( var i = 0; i < data.length - 1; i++) {
var value = data[i].substring(1) + “}”
var obj = JSON.parse(value)
wixData.insert(“List”, obj)
}
})
}
Just an idea; have you tried
$w.onReady(async function () {
var data = await fetch(’ https://jsonplaceholder.typicode.com/posts/ ')
I added what you said, and now more of the data gets added to the collection but still not all.
import wixData from ‘wix-data’;
$w.onReady( async function () {
await fetch(‘https://jsonplaceholder.typicode.com/posts/’)
.then(response => response.json())
.then(json => {
var data = json
data = JSON.stringify(data);
data = data.split(“}”)
data[0] = “,” + data[0].substring(1)
for ( var i = 0; i < data.length - 1; i++) {
var value = data[i].substring(1) + “}”
var obj = JSON.parse(value)
wixData.insert(“List”, obj)
}
})
});
When using await, waiting for the Promise to be resolved with:
.then(response => response.json()) .then(json => {
is no longer necessary.
I do not know if this might solve your problem, but I would take it out. Just get the var.json and work with that.
I tried various things and the data is not being retrieved.
$w.onReady( async function () {
var data = await fetch(‘https://jsonplaceholder.typicode.com/posts/’)
console.log(data)
data = JSON.stringify(data);
data = data.split(“}”)
data[0] = “,” + data[0].substring(1)
for ( var i = 0; i < data.length - 1; i++) {
var value = data[i].substring(1) + “}”
var obj = JSON.parse(value)
wixData.insert(“List”, obj)
}
});
Have you tried something like cURL to see if the response is valid outside Wix?
The response works for sure, some of the data gets added into the collection. I have also tried in postman and it works fine.
Hi,
Can you please share a link to your site and specify the name of the page so we can inspect?
Roi.
Hi,
You have some syntax errors.
This is your code now:
import wixData from 'wix-data';
var cars
$w.onReady(function () {
fetch('https://jsonplaceholder.typicode.com/posts/')
.then(response => response.json())
.then(json => {
var a = json
var data = JSON.stringify(a);
cars = data.split("}")
cars[0] = "," + cars[0].substring(1)
})
.then()
{
console.log(cars)
for (var i = 0; i < cars.length - 1; i++) {
var value = cars[i].substring(1) + "}"
var obj = JSON.parse(value)
console.log(obj)
wixData.insert("List", obj)
}
}
});
It should be like this:
import wixData from 'wix-data';
var cars
$w.onReady(function () {
fetch('https://jsonplaceholder.typicode.com/posts/')
.then(response => response.json())
.then(json => {
console.log('here');
var a = json
var data = JSON.stringify(a);
cars = data.split("}")
cars[0] = "," + cars[0].substring(1)
})
.then(() => {
console.log(cars)
for (var i = 0; i < cars.length - 1; i++) {
var value = cars[i].substring(1) + "}"
var obj = JSON.parse(value)
console.log(obj)
wixData.insert("List", obj)
}
})
});
Good luck!
Roi.