axios delete API does not work (with JSON server) - reactjs

I am trying to delete record from db.json through JSON server delete endpoint. db.json content is like below:
{
"todoList": [{
"todoText": "Second todo",
"key": "2"
}, {
"todoText": "Third todo",
"key": "3"
}]
}
I tried following code but it is not working. i get 404 error.
const resp = await axios.delete('http://localhost:3001/todoList?key=3') OR
const resp = await axios.delete('http://localhost:3001/todoList/', {
params: { "key": key }
})
following works if i add id property in above json (with same value like key):
await axios.delete('http://localhost:3001/todoList/' + key)
Is there a way to make it working without id property?

Related

Passing through an array with axios

I am having an issue passing through an array through axios post call. The issue is that on the api endpoint the data received is null, when I try posting using postman it works fine so the endpoint is working. Example of the array
I need to pass the data in this format:
{
"UpdateItemList": [
{
"Text": 1,
"Value": "5"
},
{
"Text": 1,
"Value": "5"
}
]
}
Code:
export function createLogEntry(postData) {
let payload = {
UpdateItemList: postData
};
const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry', {
data: payload
});
return {
type: CREATE_LOG,
payload: request
}
}
Is there any issue with the way I am passing through the data with my current code?
Try with
const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry',payload);
This worked for me!
The issue is that you are confusing two ways axios can be used. Currently you are actually posting your data nested in an object within and the key data:
{
data: {
UpdateItemList: postData
}
}
If you are using the axios.post function, you should just pass your object with the data to post as the second object like this:
const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry', payload);
If you are using the config object method, you should just pass one single object with url, method and data as keys.
// Send a POST request
axios({
method: 'post',
url: 'https://localhost:44312/api/Audit/AddLogEntry',
data: payload
});
This behaviour is explained in the axios Readme here: https://github.com/axios/axios#axios-api

Firebase Cloud Firestore - Fail to write via REST API

This is not an authentication error, write is enabled on the database rules.
My cloud Firestore database looks like the picture below.
There is a COLLECTION called colA, inside it there is a DOCUMENT called docA, and inside it there are some fields (strings) stored.
On Postman, if I do GET https://firestore.googleapis.com/v1/projects/eletronica-ab6b1/databases/(default)/documents/colA/docA, I do receive the following answer, and it is correct:
{
"name": "projects/eletronica-ab6b1/databases/(default)/documents/colA/docA",
"fields": {
"fieldB": {
"stringValue": "ABCD"
},
"fieldA": {
"stringValue": "888"
}
},
"createTime": "2020-01-31T16:48:26.859181Z",
"updateTime": "2020-02-05T19:21:49.654340Z"
}
Now, when I try to write a new field (fieldC) via POST https://firestore.googleapis.com/v1/projects/eletronica-ab6b1/databases/(default)/documents/colA/docA, with JSON content:
{
"name": "projects/eletronica-ab6b1/databases/(default)/documents/colA/docA",
"fields": {
"fieldC": {
"stringValue": "1000"
}
}
}
After SEND, I receive this:
{
"error": {
"code": 400,
"message": "Document parent name \"projects/eletronica-ab6b1/databases/(default)/documents/colA\" lacks \"/\" at index 60.",
"status": "INVALID_ARGUMENT"
}
}
What I'm doing wrong? I really would like to write strings there via REST API.
Regards.
Updating a document is done with a PATCH request, according to the [reference documentation).
A POST request is used to create a new document in a collection, which probably explains the error you get: you're pointing to a document, but POST expects a collection path.

How to compose a graphQL query from a REST Api response?

I have an API response from a REST API server which I called using my REACT + Apollo with apollo-link-rest. but the response of the API looks like this
[
[
{
"name": "joe"
"address": "123 street hello city"
},
{
"name": "joe2"
"address": "1233 street2 hello2 city"
}
],
2356
]
How can I create a query with this kind of response that has an array with a number as a last item in the array and the first item of the array consist of list of users?
So far I just have this query.
const QueryTest = gql`
query people {
people #rest(type: "People", path: "/allpersons")
}
`;
1st: you need name and address props within (inside) people query - 'ask for what you need' - to get results.
2nd: Work with apollo-link-rest usually needs type patching - technique used to adapt/transform existing (REST) responses to the current needs (structures). It can be combined with response transformer but this step isn't required.
Sometimes you need to add id (calculated from other fields, unique) for nested types (apollo normalizing cache requirement) or define additional 'response type' above desired/required type - google for type patching examples (f.e. patchDeeper usage) or tutorials... define patchers, use debugger...
It's far easier when you know what to expect/what is needed - compare responses with working graphql responses.
Had the same issue, you can use responseTransformer in your link to transform the response.
const restLink = new RestLink({
uri: '...',
responseTransformer: async response =>
response.json()
.then((data: any) => {
if (Array.isArray(data)) return { data }
else return data
})
});
So you should be able to use the query like this:
const QueryTest = gql`
query people {
people #rest(type: "People", path: "/allpersons") {
data
}
}
`;
Where data would contain:
[
[
{
"name": "joe"
"address": "123 street hello city"
},
{
"name": "joe2"
"address": "1233 street2 hello2 city"
}
],
2356
]

Cannot query ACF Options page with GraphQL and GatsbyJS

I am trying to query my ACF options page fields that I have set up in the admin side, and through the WP API Query them using GatsbyJS's gatsby-source-wordpress. When I run build and dev it requests the data and prints out the output
=== [ Fetching wordpress__acf_options ] === http://url.local/wp-json/acf/v3/options/options/
⠈ source and transform nodes -> wordpress__acf_options fetched : 1
Fetching the wordpress__acf_options took: 518.885ms
=== [ Fetching wordpress__acf_options ] === http://url.local/wp-json/acf/v3/options/options
⠈ source and transform nodes -> wordpress__acf_options fetched : 1
Fetching the wordpress__acf_options took: 478.580ms
and within those urls on the browser, I get this response. So I know the data is correct
{
acf: {
social_options: [
{
social_name: "Twitter ",
social_link: "https://twitter.com"
},
{
social_name: "Linkedin",
social_link: "https://www.linkedin.com/"
}
],
test: "pagfe 1"
}
}
The problem is when I query the allWordpressAcfOptions in the internal GraphQL. I try and see what options are available to query but all I can get is the ID. This is my query below.
{
allWordpressAcfOptions {
edges {
node {
id
internal {
type
contentDigest
owner
}
children {
id
}
wordpress_id
}
}
}
}
and none of the data in there is the data I need, an none of it is representative of the same data shown from REST API Link. So there is something missing. This is the response I get back from the Query.
{
"data": {
"allWordpressAcfOptions": {
"edges": [
{
"node": {
"id": "038ece9f-326d-5bb0-8f17-1bb72ce06c57",
"internal": {
"type": "wordpress__acf_options",
"contentDigest": "10b2729a5f87723bbafc539f99fb23d6",
"owner": "gatsby-source-wordpress"
},
"children": [],
"wordpress_id": "acf"
}
}
]
}
}
}
For reference my gatsby-source-wordpress options in the gatsby config are this below
options: {
baseUrl: 'url.local',
protocol: 'http',
hostingWPCOM: false,
useACF: true,
acfOptionPageIds: ['options'],
verboseOutput: true
},
options refers to the page ID the my ACF options page is set in my functions.php on the wordpress side.
So in summary, GatsbyJs is referencing the API and pulling the data but what am I doing wrong to query the data via GraphQL?

Passing in an array of values to an express POST route

I have a mongoose model called Todo that looks like this:
content: [{
contentType: {
type: String,
default: null
},
contentValue: {
type: String,
default: null
}
}]
My express POST route looks like this:
app.post('/todos', authenticate, (req, res) => {
var todo = new Todo({
content: req.body.content
});
res.send(todo)
//I am sending instead of sending the result for testing
});
When I send int test data using Postman the content array is coming back empty "content": []
I have tried several formats of Body in postman including:
{
"content[contentType]": "reminder",
"content[contentValue]": "Will it blend"
}
and
{
"content": {
"contentType": "reminder",
"contentValue": "Will it blend"
}
}
However both come back with empty array.
{
"content": []
}
Do I need to change something on my POST route and\or send in teh data in an alternate format?
content here is a todo sub document, so you should treat it like the following:
app.post('/todos', authenticate, (req, res) => {
var content = req.body.content;
var todo = new Todo();
todo.content.push(content);
todo.save(function(err) {
if (err) throw err;
res.json(todo.toJSON())
//I am sending instead of sending the result for testing
});
});
See: Mongoose SubDocuments

Resources