Error on Restler response in Restangular - php

I've been at this for a couple days and can't seem to get this to work. My issue is that I'm using Restler (version 3) for an API and Restangular on my front end and I'm getting the following error:
Error: can't convert undefined to object restangularizeBase#http://localhost/vendor/restangular/src/restangular.js:436 restangularizeCollection#http://localhost/vendor/restangular/src/restangular.js:552 createServiceForConfiguration/fetchFunction/<#http://localhost/vendor/restangular/src/restangular.js:610 Qc/e/j.promise.then/h#http://localhost/vendor/angular/angular.min.js:78 Qc/g/<.then/<#http://localhost/vendor/angular/angular.min.js:78 e.prototype.$eval#http://localhost/vendor/angular/angular.min.js:88 e.prototype.$digest#http://localhost/vendor/angular/angular.min.js:86 e.prototype.$apply#http://localhost/vendor/angular/angular.min.js:88 e#http://localhost/vendor/angular/angular.min.js:95 p#http://localhost/vendor/angular/angular.min.js:98 Yc/</t.onreadystatechange#http://localhost/vendor/angular/angular.min.js:99
Here are the relevant code snippets that so you can see what I'm doing:
Restler setup to set up my API
$r = new Restler();
$r->addAPIClass('User');
$r->handle();
User class object I'll be accessing for my API (for now I'm just returning an example for testing)
class User{
public function index() {
return array(
array(
'first_name'=>'John',
'last_name'=>'Smith',
'role'=>'supervisor',
),
array(
'first_name'=>'Matt',
'last_name'=>'Doe',
'role'=>'employee',
),
);
}
}
Finally my app.js file
'use strict';
var app = angular.module('cma',['restangular']);
app.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('/api');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function(response,operation) {
return response.data;
});
});
app.run(['$rootScope','Restangular',function($rootScope,Restangular) {
var userResource = Restangular.all('session');
$scope.test = userResource.getList(); // This is where the error is happening
}]);
The API is returning the following JSON response (taken from Firebug:
GET http://localhost/api/user 200 OK 96ms
):
[
{
"first_name": "John",
"last_name": "Smith",
"role": "supervisor"
},
{
"first_name": "Matt",
"last_name": "Doe",
"role": "employee"
}
]
I can't see anything that is going on that would be causing an issue. Any help would be greatly appreciated!

I'm the creator of Restangular :)
The problem is that you're using a responseInterceptor and actually returning an array.
So, your server is returning an array. Your responseInterceptor gets it and then returns the data variable of your array. As that's undefined, undefined is sent to Restangular and therefore you get that error.
Remove the responseInterceptor and everything will start working :).
Bests

Related

The argument type 'String' can't be assigned to the parameter type 'Uri'. Flutter

I'm having a trouble, why I'm getting this error in my app flutter main.dart
I just follow this reference link. It would be great if anybody could figure out, thank you so much in advance!.
Here's the error part
Future<List> _login() async {
final response = await http.post("http://10.0.2.2/my_store/login.php", body: {
"username": user.text,
"password": pass.text,
});
You need to parse your URLs now, use it like this:
Future<List> _login() async {
final response = await http.post(Uri.parse("http://10.0.2.2/my_store/login.php"), body: {
"username": user.text,
"password": pass.text,
});

How to post data in angular

This is the angular code i used to post data
const postData = {
"username": "Customer004",
"email": "customer004#email.com",
"password": "0000252525",
"gender": "male",
}
this.http.post("http://localhost/android/Api.php?apicall=signup", postData, options)
.subscribe(data => {
console.log(data);
});
But when i tried to print the response i am getting null in the backend using php code
response['post']=$_POST['username'];
it is giving me null value
this is my php code..
I have hardcoded the data.But trying to print post request.It is giving me null
This is my php code
https://pastebin.com/raw/VishzAvG
My angular code is here
https://pastebin.com/raw/LNk3KvYW
At the backend in php use
$post=json_decode(file_get_contents('php://input'));
echo $post->username;
It'll work

Wallet Pass Update - The pass cannot be read because it isn’t valid

I have implemented passbooks and updating. I'm using the eo/passbook-bundle and have created below JSON when packaging the passbook without errors, but when, in the update process, the server calls the webServiceURL/version/passes/passTypeIdentifier/serialNumber api, and the device downloads exactly the same JSON (but not packaged, just this json), it fails.
I've read in a comment on Received invalid pass data while updating Apple Wallet Pass that it may have something to do with correct headers, but there's no description whatsoever in the docs on any specific headers to return in the response (see https://developer.apple.com/library/content/documentation/PassKit/Reference/PassKit_WebService/WebService.html)
I'm using symfony to return the response with the encoded json like this:
$json = PassFactory::serialize($storecard);
return new Response($json, 200, ['Content-Type' => 'application/json']);
I've also tried it without the Content-Type header.
In the log I can see: Get pass task (pass type pass.com.spinpos.latest.customercard, serial number 5, if-modified-since (null); with web service url https://my.domain.com/api/loy/wallet/) encountered error: Received invalid pass data (The pass cannot be read because it isn’t valid.)
The JSON response is this:
{
"storeCard":{
"headerFields":[
{
"key":"prepaid",
"value":"0,00",
"label":"PREPAID"
}
],
"secondaryFields":[
{
"key":"username",
"value":"Some Name",
"label":"GEBRUIKERSNAAM"
},
{
"key":"lastvisit",
"value":"niet bekend",
"label":"LAATSTE BEZOEK"
}
],
"auxiliaryFields":[
{
"key":"qrcode",
"value":"2690310109104",
"label":"QR CODE"
},
{
"key":"points",
"value":20,
"label":"LOYALTY PUNTEN"
}
],
"backFields":[
{
"key":"website",
"value":"http://www.somesite.com",
"label":"WEBSITE"
},
{
"key":"phone_number",
"value":"05311223344",
"label":"TELEFOONNUMMER"
},
{
"key":"terms_of_service",
"value":"Dit zijn test algemene voorwaarden!",
"label":"ALGEMENE VOORWAARDEN"
}
]
},
"serialNumber":"5",
"description":"Your Customer card",
"formatVersion":1,
"locations":[
{
"latitude":51.62,
"longitude":5.55,
"relevantText":"Some Loc 2"
},
{
"latitude":51.7737967,
"longitude":5.5513203,
"relevantText":"Some loc 1"
},
{
"latitude":52.2648569,
"longitude":6.8018085,
"relevantText":"Testdfdr"
}
],
"barcode":{
"format":"PKBarcodeFormatQR",
"message":"2690310109104",
"messageEncoding":"iso-8859-1"
},
"barcodes":[
{
"format":"PKBarcodeFormatQR",
"message":"2690310109104",
"messageEncoding":"iso-8859-1"
}
],
"backgroundColor":"rgb(255,108,108)",
"foregroundColor":"rgb(46,14,170)",
"labelColor":"rgb(110,255,75)",
"authenticationToken":"048cfd30e4baf1c3250f0c8c7500f998aee2da49",
"webServiceURL":"https://my.domain.com/api/loy/wallet/"
}
Anyone any idea what's wrong with the JSON or the headers?
You need to send an entire new, signed .pkpass bundle with a header application/vnd.apple.pkpass

Facebook API GET recent post ID

I'm currently working on a little PHP 'script' that will automatically grab the Post ID from the most recent post on a Facebook page.
I have so far got this:
$status = $facebook->api("/645017715510822/feed?fields=id&limit=1&access_token=".$token, 'GET');
When I run this through my browser as https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX then it will display the information that I need:
{
"data": [
{
"id": "645017715510822_1484080338478440",
"created_time": "2014-04-07T12:15:32+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX&since=1396872932&__previous=1",
"next": "https://graph.facebook.com/645017715510822/feed?fields=id&limit=1&access_token=XXXXXX&until=1396872931"
}
}
What I am needing is for it to then grab only the 'id' and possibly print it?
My knowledge of PHP isn't wonderful and I've already tried searching related posts on here, but can't seem to find anything for my exact request, any assistance would be greatly appreciated.
Thanks.
Try this-
if( !empty($status['data']) )
{
foreach($status["data"] as $s)
{
$id = $s["id"];
}
}
else
{
//error
}

Acessing a JSON Object using Javascript

I have the following JSON object
({
"codes": [ {
"code": {
"redeemed": "true", "retailer": "R1", "code": "ab11845b55b3ef1c3f137a35dbdfb270"
}
}, {
"code": {
"redeemed": "false", "code": "48c02f7bd35271de0aa215209b0a390f", "message": "code already used"
}
} ]
});
for (var code in data.codes) {
console.log(code) // prints 0
console.log(code[0].id) // prints 0
}
How can I access the inner objects separately ?
To output each individual "code" object:
// First, assign your data to a variable:
var data = ({
"codes": [ {
"code": {
"redeemed": "true", "retailer": "R1", "code": "ab11845b55b3ef1c3f137a35dbdfb270"
}
}, {
"code": {
"redeemed": "false", "code": "48c02f7bd35271de0aa215209b0a390f", "message": "code already used"
}
} ]
});
// (I use the "c" temprary variable here because it's shorter)
var c = data.codes;
// Loop through all the codes.
for (var i = 0; i < c.length; i++) {
// Log the specific code at [i].
console.log(c[i].code); // Output a "code" object.
}
You can then use:
c[i].code.redeemed;
c[i].code.retailer;
c[i].code.code;
c[i].code.message;
These will however return undefined when they're not set as a property of the specific "code" object.
Working example
assuming what you had added:
window.json_obj = ({"codes":[{"code":{"redeemed":"true","retailer":"R1","code":"ab11845b55b3ef1c3f137a35dbdfb270"}},{"code":{"redeemed":"false","code":"48c02f7bd35271de0aa215209b0a390f","message":"code already used"}}]});
console.log(json_obj.codes[0]['code']['code']);
// or
// console.log(json_obj.codes[0].code.code);
jsFiddle demo
console.log( obj.codes[0].code.retailer );
console.log( obj.codes[1].code.message );
Outputs
R1
code already used
None of the objects have an id property.
First:
Your JSON should not be in the parens, if you have no reason for it to be:
(<JSON>);
If that's all you're doing, it isn't going to let you access it.
You need to assign it to a variable...
var data = { codes : [ { code : "...", ...}, ... ] };
Once you do that, you have access like:
data.codes[i].code.code;
data.codes[i].code.redeemed;
PS: if it's not a full string
'{ "codes":[{"code":"...",...},...]}'
then it's not a JSON object, it's a JavaScript object.
Yes, that sounds picky.
But it all comes down to if you need to parse it and how, before you can use it properly.
the for (var stuff in whatever) returns you the index of the object.
So basically, to access your object you have to do
console.log(data.codes[code]);
console.log(data.codes[code].id);
and please format your json in a readable way next time, hard to read here :)

Categories