error when creating an array: Object of class stdClass could not be converted to string - laravel-6.2

I was creating an array using labels and creating arrays inside arrays, when Laravel 6.2 gave me this error:
Object of class stdClass could not be converted to string
It's not syntax error.
[
'product_id'=>(string)$productId,
'name'=>
'SubscriptionPlan',
'description'=>
'SubscriptionPlan',
'status'=>'ACTIVE',
'billing_cycles'=>[
0=>[
'frequency'=>[
'interval_unit'=>'MONTH',
'interval_count'=>1,
],
'tenure_type'=>'REGULAR',
'sequence'=>2,
'total_cycles'=>999,
'pricing_scheme'=>[
'fixed_price'=>[
'value'=>(string)$p->monthlyFee,
'currency_code'=>'USD',
],
],
],
],
'payment_preferences'=>[
'auto_bill_outstanding'=>true,
'setup_fee'=>[
'value'=>'0',
'currency_code'=>'USD',
],
'setup_fee_failure_action'=>'CONTINUE',
'payment_failure_threshold'=>3,
],
'taxes'=>[
'percentage'=>'10',
'inclusive'=>false,
],
];

You're casting two variable to string:
$productId
$p->monthlyFee
One of them is an object.
You can't cast object to string this way.
You should try to dump both variables to see which one is the cause of the problem.
Happy new year !

Related

Meteor query based on the value of elements in an array inside an object

I am new to meteor and mongoDB and have been searching for an answer to this question for some time without any luck.
I have multiple documents in MongoDB similar to the one below:
{
"_id" : ObjectId("5abac4ea0c31d26804421371"),
"Points" : [
{
"Value" : 6.869752766626993,
"Time" : 1522284528946
},
{
"Value" : 3.9014587731230477,
"Time" : 1522284543946
},
{
"Value" : 1.2336926618519772,
"Time" : 1522284558946
},
{
"Value" : 6.504837583667155,
"Time" : 1522284573946
},
{
"Value" : 9.824138227740864,
"Time" : 1522284588946
},
{
"Value" : 9.707480757899235,
"Time" : 1522284603946
},
{
"Value" : 4.6122167850338105,
"Time" : 1522284618946
}
]
}
How can I implement a query in meteor that returns an array containing all the Points from all documents with 'Time' field greater than certain value?
As Jankapunkt has pointed out in his comment, it might be a lot easier and better if you created a new collection Points where each document includes only Value and Time attributes. The given example would then become seven separate documents rather than a single array.
It does nevertheless happen, that we want to query documents according to some inner values, e.g. attributes in objects in arrays.
Taken from the mongodb documentation on querying embedded documents, we can just use dot notation for this.
If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document.
Such as for your question (assuming Points to be the name of your collection):
db.points.find( { 'Points.Time': { $gte: 123412341234 } } )
Which looks almost identical in Meteor:
Points.find({ 'Points.Time': { $gte: 123412341234 } })

How to handle inner Json when using JsonOutputter

I'm converting some csv files into Json using the JsonOutputter. In the csv files I have a field containing Json like this (pipe character is delimiter):
...|{ "type":"Point", "coordinates":[ 18.7726, 74.5091 ] }|...
When it's output to Json, the result looks like this:
"Location": "{ \"type\":\"Point\", \"coordinates\":[ 18.7726, 74.5091 ] }"
I would like to get rid of the outer quotes to make the Json look like this:
"Location": { "type":"Point", "coordinates":[ 18.7726, 74.5091 ] }
What is the best way to accomplish this? The output Json will be stored in Cosmos DB, so I guess the "cleaning up" of the Json could be done either in U-SQL or in Cosmos DB?
The sample outputter is only generating flat JSON. Since we do not have a JSON datatype, any string value has to be escaped to be a string value.
You can write your own custom Outputter that for example takes SqlMap instances for nested values and output them as nested JSON, or - if you know that some strings in the rowsets are really JSON and not just strings, serialize them without the quotes.
If JsonOutputter is not the only choice to that
,we could covert csv file to Json with our custom code.
I test it with following csv file.
number|Location
1|{ "type":"Point", "coordinates":[ 13.7726, 73.5091 ] }
2|{ "type":"Point", "coordinates":[ 14.7726, 74.5091 ] }
Please have a try to use the following code, it works correctly on my side.
var lines = File.ReadAllText(#"C:\Tom\tomtest.csv").Replace("\r", "").Split('\n');
var csv = lines.Select(l => l.Split('|')).ToList();
var headers = csv[0];
var dicts = csv.Skip(1).Select(row => headers.Zip(row, Tuple.Create).ToDictionary(p => p.Item1, p => p.Item2)).ToArray().Select(x=>new
{
number = x["number"],
location = JObject.Parse(x["Location"])
});
string json = JsonConvert.SerializeObject(dicts);
Console.WriteLine(json);
Test result:

Groovy Map Issue with Variable Properties and String INterpolation

I have been navigating map structures fine for a long time now. Yet, for some reason, the root of this problem escapes me. I've tried bracket notation as well, no luck.
Why doesn't the final output (null) return "[serverinfo:[listenPort:19001]]"
If I replace the two instances of ' "$instanceName" ' with simply ' services ', it works.
String instanceName = "Services"
Map serverNode = [
instances:[
"$instanceName":[
serverinfo:[
listenPort:19001
]
]
]
]
println "$instanceName"
println serverNode.instances
println serverNode.instances."$instanceName"
//output
Services
[Services:[serverinfo:[listenPort:19001]]]
null
The type of "$instanceName" is GStringImpl, not String. It's a common mistake (and hard to find!)
def serverNode = [
instances:[
("$instanceName" as String):[
serverinfo:[
listenPort:19001
]
]
]
]
as stated by #tim_yates in comment, if your interpolated string is as simple as in this example (ie ,"${property}"), then you can use the (property) syntax : Groovy put the value of the property as a key, not the word "property"

Doctrine 2 Spatial Data

I am having extreme difficulty making this doctrine2 extension works.
It is https://github.com/djlambert/doctrine2-spatial and theres not a lot of doc on how to create a polygon. I got the config file working and all but I am struggling with creating the actual polygon.
array:564 [
0 => array:2 [
0 => -73.698313
1 => 45.546876
]
1 => array:2 [
0 => -73.69813
1 => 45.546916
]
2 => array:2 [
0 => -73.697656
1 => 45.546899
]
3 => array:2 [
0 => -73.697413
1 => 45.546899
]
$poly = new Polygon($array);
[CrEOF\Spatial\Exception\InvalidValueException]
Invalid Polygon Point value of type "double"
This is the actual error Im getting. I tried creating points instead because apparently it doesn't like doubles.
$p = new Point($coord);
$temp[] = $p;
$poly = new Polygon($temp);
[CrEOF\Spatial\Exception\InvalidValueException]
Invalid Polygon LineString value of type "CrEOF\Spatial\PHP\Types\Geometry\Point"
After that, I was like ok, lets create a line string object and pass it.
$line = new LineString($points);
$poly - new Polygon($line);
[Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to CrEOF\Spatial\PHP\Types\AbstractPolygon::__construct() must be of the type array, object given, called in /Library/Web Server/Documents/mg/src/Momoa/ImmobilierBundle/Entity/geography/Quartier.php on line 131 and defined
Im just lost right now, the only thing I wanted was to store polygons in the database and call spatial functions such as CONTAINS. Do you have any recommendation or such other things to make all of this work.
After digging through the source code I found this validate function which seems to be the problem
case (is_array($point) && count($point) == 2 && is_numeric($point[0]) && is_numeric($point[1])):
return array_values($point);
break;
default:
throw InvalidValueException::invalidType($this, GeometryInterface::POINT, $point);
}
The way I'm understanding this is that the extension do not accept points that have decimal values ?! Huh, Does that mean I need to convert my coordinates to 2 integers ?!
I will post the solution I found. Basically you need to create your polygon like this
$line = new LineString($coords);
$poly = new Polygon(array($line));
//Or you can do it like this
$coords[0] = $coords;
$poly = new Polygon($coords);
//Following if you wanna use MBRContains or Contains
$dql = "SELECT p FROM polygon p WHERE MBRContains(p.geometry, GeomFromText('Point($lat $lng)'))=1";
//Dont use GeomFromText(:point), and then $point = new Point(array($lat,$lng));
Basically good luck guys, that library is useful but the documentation is BAD !
Spent the whole day on that yesterday !!
You can create it by passing data in constructor.
But the problem is that you should have a valid polygon data:
$p = new Polygon([[[lat1, lng1], [lat2, lng2], [lat3, lng3], [lat1, lng2]]]);
Be sure that in the Polygon
there are three arrays — array of lines, where line is an array of points, where every point is an array.
the path is closed — the last point equals to the first one.
The same truth for MultiPolygon but + one more array. F.e.
[[[["32.699005219026645","-117.18222600929262"],["32.694563070816095","-117.18437177650453"],["32.697687043641835","-117.17149717323305"],["32.699005219026645","-117.18222600929262"]]]]

Adding values to the keys in Map

I have a record
record = [ [ name1:'value1', name2:'value2', name3:'value3' ],
[ name1:'value6', name2:'value7', name3:'value8' ] ]
I would like to add two more key/value pairs to with values as boolean(true/false) as below
record = [ [ name1:'value1', name2:'value2', name3:'value3', name4:false, name5:true ],
[ name1:'value6', name2:'value7', name3:'value8', name4:false, name5:true ] ]
When I tried to use add or put functions, doesnt seem to work(either replacing the existing values or not doing anything)
Just do:
record = record.collect { it + [ name4:false, name5:true ] }
Or you can also do:
record = record*.plus( name4:false, name5:true )
To add to Patricks answer above (+1), a Map contains a set not a list so all keys must be unique. So you cannot assign multiple values to a single key directly.
Among many solutions, you can alternatively, save an object:
Map<String, myObject>
that holds many different values and this will still maintain the uniqueness of the set since there will only be one key.

Resources