Axios post method refreshes the page - javascript

So I got this problem whenever I post something in my database on localhost with Axios, the page gets refreshed automatically. Searched the internet for the same problem, but didn't really find anything, everything was on the HTML form things and I don't really have a form on my page.
codepen: https://codepen.io/shotaivanidze/pen/PooLNPZ
Here is a JSON database
{
"tags": [
{
"title": "test 1",
"id": 1
},
{
"title": "test 2",
"id": 2
},
{
"title": "test 3",
"id": 3
},
{
"title": "test 4",
"id": 4
},
{
"title": "test 5",
"id": 5
},
{
"title": "test 6",
"id": 6
},
{
"title": "test 7",
"id": 7
}
]
}

Related

Selected item in createApi generated reducer

Currently, I have successfully built a redux-toolkit api using following code:
const api = createApi({
reducerPath: 'api',
baseQuery: baseQueryWithReauth,
endpoints: (builder) => ({
getClusters: builder.query({
query: ROUTES.CLUSTERS,
}),
}),
})
The data Im receiving from backend looks like this:
[
{
"name": "test 1",
"description": "test 1",
"id": 1
},
{
"name": "test 2",
"description": "test 2",
"id": 2
}
]
The user should be able to select one of these items through the interface, and the selection should be saved at redux. Something like:
{
"data": [
{
"name": "test 1",
"description": "test 1",
"id": 1
},
{
"name": "test 2",
"description": "test 2",
"id": 2
}
],
"selectedItem": 1 // Saved selected item id in redux so I can use it globally!
}
I have tried using createEntityAdapter and adding the selectId adapter, but seems not supported (I dont even know if its the correct approach). Is there any way to do it using redux-toolkit capabilities?

elasticsearch.js bulk insert error

I am trying to insert/update data with the javascript elasticsearch client, but I am getting the error:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
}
],
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
},
"status": 400
}
This is the data that is being sent
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data":
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
]
})
It appears as this code generates the following request to ES:
-> POST http://docker.me:9200/_bulk
{
"index": {
"_index": "myindex",
"_type": "movie",
"_id": "1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data": {
"title": "Title 2",
"description": "This should be updated with this new.",
"score": 1,
"suggest_title": "Title 2",
"img": "http://url.to.image/img.jpeg",
"genres": [
"Comedy"
],
"release": "2015-01-07T23:00:00.000Z",
"language": "EN",
"provider": {
"id": "InstaFilmFlixify",
"url": "http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
What am I doing wrong? What is happening? Can this possibly be a bug in ES / the ES adapter.
Elasticsearch version 2.1
I haven't seen the "_data" parameter before. Where did you get the idea to use that?
Take a look at the docs for the js client.
Anyway, this should work for you:
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
}
},
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
]
})

Passing function argument to retrieve data from an object

I am have some trouble with a script I am working on. I have been provided with an object with multiple items from a product catalog.
What I am trying to do is to write a function which to which will allow me to render this data easily.
<script type="application/javascript">
SKUinfo =
{
"s238554": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/icon18.gif"
},
"Barcode": {
"Barcode": "50622132430794"
},
"Currency": "£",
"Description": "Description goes here",
"Id": 44305,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "Xbox 360",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
}
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
},
"s238556": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/pegi/icon18.gif"
},
"Barcode": {
"Barcode": "5060134530794"
},
"Currency": "£",
"Description": "Description here",
"Id": 654654,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "PlayStation 3",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
},
{
"ScreenshotMax": "/productImages/238556/7scrmax2.jpg",
"ScreenshotMin": "/productImages/238556/6scrmin2.jpg"
},
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
"VideoHTML": "html here",
"status": {
"Response": "product found",
"Success": true
}
}
}
</script>
The above example is the output I get for two products.
If I try to get access to this data this is where I have a problem
<script type="application/javascript">
function getSKU(s)
{
console.log(SKUinfo.s.Title);
}
getSKU(s238554);
</script>
I imagine this is being caused when I am passing the argument s back to the function getSKU a the node selection in the data object. In this I would expect the console output to be the Title from SKU s238554.
What I get however, is: Uncaught ReferenceError: s238554 is not defined
I would appreciate any guidance that can be offered as I am a javascript novice.
Access your property by used[] on SKUinfo.s.Title like SKUinfo[s].Title
And also pass your property name within the quotes 's238554' as it's not variable.
Something like this.
function getSKU(s){
console.log(SKUinfo[s].Title);
}
getSKU('s238554'); // s238554 within quotes.

Ember Data belongsTo Association (JSON format?)

I have two models 'Author' and 'Publisher' (Rails), with a publisher hasOne author / author belongsTo publisher relationship.
I have the Ember models setup correctly -- JS Fiddle -- and the associations working when I manually push into the store. But only the publisher records are created when requesting /publishers index.
I've tried several types of JSON responses:
Publishers with author
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": 1
}
],
"author": {
"id": 1,
"name": "Test A 1",
"publisher": 1
}
}
Publishers with authors
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": 1
}
],
"authors": [{
"id": 1,
"name": "Test A 1",
"publisher": 1
}]
}
Publishers with author embedded
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author": {
"id": 1
"name": "Test A 1"
}
}
]
}
Thanks for any help!
The ActiveModelAdapter/ActiveModelSerializer expects _id/_ids to be appended on relationships
{
"publishers": [
{
"id": 1,
"name": "Test P 1",
"author_id": 1
}
],
"authors": [{
"id": 1,
"name": "Test A 1",
"publisher_id": 1
}]
}
http://jsfiddle.net/6Z2AL/1/
Adding a link to ember-data issue in case it helps anyone -- single object push payload

how to get the user id from this array and print that

i have code to get the my friend list from facebook
the code is................
function getFriends() {
//if the person has not pressed login button
if(!loggedIn) {
loginFacebook();
}
document.getElementById("status").innerHTML = "Now loading your friends' id...";
//if the person is loggedIn
if(loggedIn) {
document.getElementById("friendBtn").disabled = "disabled";
FB.api("/me/friends",function(response){
friends = response["data"];
totalToBeLoaded = friends.length;
// addNewRow("Name","id");
loadid(friends.length);
});
}
}
the response["data"] function returns the data in the form of array as
{
"data": [
{
"name": "name 1",
"id": "123456781"
},
{
"name": "name 2",
"id": "123456782"
},
{
"name": "name 3",
"id": "123456783"
},
{
"name": "name 4",
"id": "123456784"
},
{
"name": "name 5",
"id": "123456785"
},
{
"name": "name 6",
"id": "123456786"
}
]
}
now i need to get that name and id of the user from that response["data"] and print that .. how to do that?
Its an array of objects. You can use the following:
var list=response["data"];
for(var i=0;i<list.length;i++) {
addUser(list[i].name, list[i].id);
}
EDIT:
Even first line can be written as
var list=reponse.data;

Categories

Resources