AngularJS $http patch request not sending data - javascript

The data is being passed to the replyMessage as the console log is showing the correct data, however, the API isn't receiving this data. Input is empty?
replyMessage: function(data) {
console.log(data);
return $http.patch('/api/email/inbox/0', data);
}
Can you see any issues with this or any things to look at?

Are you formatting patch correctly?
It needs to be a JSON.stringify(data) Array.
With data being formatted like so:
[
{
"op" : "replace",
"path" : "/Name", // <-- this is what field you're editing
"value" : "John Doe"
}
]
Also you might need your contentType to be application/json-patch+json;

Related

JavaScript url in json returns 501 but string url does not

So I have the following problem:
I try to pull data from iCloud using CloudKit (HTTP requests)
The iCloud entity contains a CKAsset and I need the download URL which a POST request to https://api.apple-cloudkit.com/database/1/... returns. However, if I try to download the data from this URL, it returns a 501 error but if I print the URL to the console and paste that into the browser, the browser downloads the file.
It gets even weirder because if I implement the URL I printed to the console directly into the code everything works! I am converting the URL in the JSON response to a string so IDK what's wrong.
CloudKit response:
{ "records" : [ { "recordName" : "xxxxxxxxxxxx", "recordType" : "xxxxxxx", "fields" : { "file" : { "value" : { "fileChecksum" : "AZJ1FbmpL7caqaksfwrFm3586o5+", "size" : 303, "downloadURL" : "https://cvws.icloud-content.com/B/AZJ1Fbmpxxxaxxxxxxx/${f}?xxxxxxxx..." }, "type" : "ASSETID" },},} ] }
I shorted the response so that it only contains the relevant stuff.
I tried to get the URL with the following code: var url = data["records"][0]["fields"]["file"]["value"]["downloadURL"];
Already tried with .toString() and var url == "" + data["records"]....
It works if I do var url = "https://cvws.icloud-content.com/B/AZJ1Fbmpxxxaxxxxxxx/${f}?xxxxxxxx..." but obviously this is no real solution.
Help is really appreciated!
EDIT:
Here is the code that downloads the file from downloadURL. I'm using a library called zip.js because the file is a zip file (with a different file extension):
zip.createReader(
new zip.HttpReader(url),
function (reader) {
reader.getEntries(async function (entries) {
if (entries.length) {
entries[0].getData(
new zip.TextWriter(),
async function (text) {
reader.close(function () {
// onclose callback
});
},
function (current, total) {
// onprogress callback
}
);
} else {
}
});
},
function (error) {
// onerror callback
}
);
EDIT 2:
I found out something that might be interesting: If I paste the URL directly into the code, status code 200 returns from disk cache. I tried loading the website in incognito mode and I had to reload once to get it working. Because I receive a new download ID on every refresh, it can't cache a status code.
This works if the URL is valid
You need to look in the network tab to see what is the matter. For example the ${f} looks suspicious
const data = {
"records": [{
"recordName": "xxxxxxxxxxxx",
"recordType": "xxxxxxx",
"fields": {
"file": {
"value": {
"fileChecksum": "AZJ1FbmpL7caqaksfwrFm3586o5+",
"size": 303,
"downloadURL": "https://cvws.icloud-content.com/B/AZJ1Fbmpxxxaxxxxxxx/${f}?xxxxxxxx..."
},
"type": "ASSETID"
},
},
}]
}
location = data.records[0].fields.file.value.downloadURL;
The code that you have above does work to pull the URL out of the object (run code below). Are you sure that data holds the information in the format you're expecting and that the URL is correct?
var data = { "records" : [ { "recordName" : "xxxxxxxxxxxx", "recordType" : "xxxxxxx", "fields" : { "file" : { "value" : { "fileChecksum" : "AZJ1FbmpL7caqaksfwrFm3586o5+", "size" : 303, "downloadURL" : "https://cvws.icloud-content.com/B/AZJ1Fbmpxxxaxxxxxxx/${f}?xxxxxxxx..." }, "type" : "ASSETID" },},} ] };
// Original
var url = data["records"][0]["fields"]["file"]["value"]["downloadURL"];
// Object notation
var url2 = data.records[0].fields.file.value.downloadURL;
console.log(url);
console.log(url2);
Ok so I found a solution. This has nothing to do with the actual URL, it is fine, but rather the way how zip.js downloads data. It first makes a HEAD request and because the iCloud servers do not support these, it returns 501. The static domains worked because it somehow cached 200 OK, even though 501 returned.
Thank you to everyone for their help!

Can't access nested json elements with javascript

I'm really stuck on how to access nested elements of a json response that I'm getting from my api. I validated the json and it shows as valid.
For instance, how do I access the numFound attribute? Here's my code that is not working:
$.ajax({
url: "/api/SearchAPI/infopop?id=" + songID,
datatype:"json",
method: "get"
}).done(function (data) {
var obj = JSON.parse(data);
alert(obj); /* This displays the entire json response
alert(obj.response.numFound) /* This does not work
alert(data.response.numFound) /* This does not work
Here's the response I'm trying to access
{
"responseHeader":{
"status":0,
"QTime":0,
"params":{
"q":"*:*",
"indent":"off",
"fl":"Name,Description,Keywords,ISRC,Instruments,Lyrics,Bpm,Vocal,Tempo,Key,TV_Genres,Music_Genres,length\r\n,Writers,profileImagePath\r\n,Publishers\r\n,songImagePath\r\n,Band_Styles",
"start":"0",
"callback":"?\r\n",
"fq":[
"id:00106c8c-7e21-4e75-80da-cdff8e6d3d44",
"publicflag:1",
"pubname:komposed"
],
"rows":"10",
"version":"2.2",
"wt":"json"
}
},
"response":{
"numFound":1,
"start":0,
"docs":[
{
"profileImagePath":[
"https://komposed.blob.core.windows.net/jrock-1873564a-d409-4370-80d8-23dc97114f18/songimage/f557110f-4ad3-4353-a1b3-3ba70d52e8f0?sv=2014-02-14&sr=b&sig=Q3ywrwEa6URp%2FPCvK0Ngesza8PBhMEmE5ONeKhw8vE4%3D&st=2016-06-16T16:49:45Z&se=2066-06-16T16:54:45Z&sp=r&rsct=application%2Foctet-stream&rscd=attachment%3B%20filename%3Dredneck.jpeg"
],
"songImagePath":[
"https://komposed.blob.core.windows.net/jrock-1873564a-d409-4370-80d8-23dc97114f18/songimage/f0c00dcd-69f9-42af-b236-4a53f0d78e76?sv=2014-02-14&sr=b&sig=oF0STMDddyuJiNZO%2BE78sYtbboC4ic%2Fl4bR5ESBFouE%3D&st=2016-06-16T16:48:59Z&se=2066-06-16T16:53:59Z&sp=r&rsct=application%2Foctet-stream&rscd=attachment%3B%20filename%3DTENSION%20LAST%20MAN.jpg"
],
"Name":[
"Bottle Service Tension"
],
"Description":[
"Cool crime scene track with a chill night club vibe"
],
"Bpm":[
90
],
"Vocal":[
"Acapella"
],
"Tempo":[
"Fast"
],
"Writers":[
"Justin Sirota|100.00|"
],
"Keywords":[
"club, crime, investigation, cool, lounge, pulse"
],
"TV_Genres":[
"Tension"
]
}
]
}
}
Just use obj.numFound. response is your data argument.
$.ajax({
...
}).done(function (response) {
alert(response.numFound)
...
Also, since you're telling jQuery (assuming this based on the code) that the response is of JSON type via datatype:"json", you don't need to do JSON.parse on it.
Turns out the issue was in my API. I was returning json as a string instead of an object. Once I did that, javascript was able to reference the json elements. Thanks for trying to help!

Restangular getList() method not returning JSON

I have problem with geting a data from Restangular promise. I always get a promise instead of pure data in JSON.
This is response from my API
localhost:3000/api/meal
{
"status": "success",
"data": [
{
"meal_id": 4,
"meal_type_id": 2,
"description": "blahblah",
"price": "3.50",
"info": "120/120/20g",
"restaurant_id": 2
},
...
...
}
],
"message": "Retrieved ALL meals"
}
This is my config method for extracting data from response
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
var extractedData;
// .. to look for getList operations
if (operation === 'getList') {
// .. and handle the data and meta data
return data.data;
} else {
extractedData = data.data;
}
return extractedData;
});
This is how I am trying to get data from my API
Restangular.all('meal').getList().then(function(meals) {
$scope.menu = meals; //meals.plain()
console.log($scope.menu);
});
but i always get this response
I need just JSON array from "data" field for using in my application.
Sry guys, after hours of research and debuging I found bug in my backend API in one specific select. Now Express.js, pg-promise and application works correctly.

Expected response to contain an array but got an object

So I'm new in Angular, and I've looked over various other solutions, but no one seemed to work for me. My app needs to take some data from mongodb database and show it to the client. The thing is I get
Error: [$resource:badcfg] Error in resource configuration for action query. Expected response to contain an array but got an object
Here is my SchoolCtrl.js on the client
app.controller('SchoolsCtrl', function($scope, SchoolResource) {
$scope.schools = SchoolResource.query();
});
Here is my ngResource
app.factory('SchoolResource', function($resource) {
var SchoolResource = $resource('/api/schools/:id', {id: '#id'}, { update: {method: 'PUT', isArray: false}});
return SchoolResource;
});
This is my SchoolsController on the server
var School = require('mongoose').model('School');
module.exports.getAllSchools = function(req, res, next) {
School.find({}).exec(function(err, collection) {
if(err) {
console.log('Schools could not be loaded: ' + err);
}
res.send(collection);
})
};
I tried adding IsArray: true, tried adding [] after 'SchoolResource' in the resource, tried changing routes, nothing worked. I wanted to see what actually is returned, that the query complained is not array, so I turned it to string and this was the result:
function Resource(value) { shallowClearAndCopy(value || {}, this); }
I have no idea why it returns a function. Can anyone help me?
That error message usually means your server is returning JSON representing a single object, such as:
{"some": "object", "with": "properties"}
when Angular is expecting JSON representing an array, like:
[ {"some": "object", "with": "properties"}, {"another": "object", "with": "stuff"} ]
Even if there is only a single result, query expects JSON for an array:
[ {"a": "single", "result": "object"} ]
You can verify this by simply loading your API call into the browser and checking it out. If there aren't square brackets around the whole JSON response, it isn't an array.
It happened to me too, but then I printed the object in the console and found out that there was something like this:
{ options:{....},results:[ ...the Array I was looking for... ]}
so all you need to do there is
res.send(collection.results);
I hope this helps

Not able to set text in input field from json response

I have a form in which I have to fill data after I have got a json object from httpGet in javascript.
$("#getDetails").click(function() {
$.get("/servlet",{
mID : 5
})
.done(function(data) {
$("#input1").val(data["some key"]);
$("#input2").val(data.name);
$("#input3").val("directvalue");
});
});
In the above 3 fields, only input3 gets filled with "directvalue".
Is there some problem in accessing json object or in setting value of input fields.
Note: the json object contains keys with spaces like "some key":"some value"
edit:
When I tried Object.keys(data)[index] to access the object field, I got Uncaught TypeError: Object.keys called on non-object
Try This
$("#getDetails").click(function () {
$.get("/servlet", {
mID: 5 })
.success(function (data) {
$("#input1").val(data.d.somekey);
$("#input2").val(data.d.name);
$("#input3").val("directvalue");
});
});
In the comments, you say it's returning this object:
{
"Name": "my name",
"my address": "23,round street",
"Description": "PM Speech at Red Fort on Indep Day 2014"
}
Try:
$("#input2").val(data.Name);
When I did console.out(data), it was printing a json object.
But when I tried Object.keys(data)[index] to access the object field, I got Uncaught TypeError: Object.keys called on non-object.
This means that dataitself was not a json object.
So I got a hint that server was giving a string in which json object was written. I had to simply parse the json object from the data string.
Following is the working code:
$("#getDetails").click(function() {
$.get("/servlet",{
mID : 5
})
.done(function(data) {
var dataObj=JSON.parse(data);
$("#input1").val(dataObj["some key"]);
$("#input2").val(dataObj.name);
});
});

Categories

Resources