Remove value for a key on a PFObject - parse-platform

I am trying to update an object based on an edit object form that I provided to the user. Whenever the value for a field is set to "" It gives me an error Can't use nil for keys or values on PFObject. Use NSNull for values. I have tried a couple of other things like self.Asset!["Manufacturer"] = nil, self.Asset!["Manufacturer"] = "" and self.Asset!.remove(forKey: "Manufacturer") All of these gave me the same error. Asset is a PFObject and Manufacturer is a key for that object. Below is the sample code for a field
if(ManufacturerLabel.text != ""){
self.Asset!["Manufacturer"] = ManufacturerLabel.text
}else{
self.Asset!.remove(forKey: "Manufacturer")
//self.Asset!["Manufacturer"] = nil
}
P.S. Please do not downvote it as I already checked the Parse Documents and other similar questions and nothing seems to work here.

As the error says, you need to use NSNull. Try:
self.Asset!["Manufacturer"] = NSNull()

Related

Get to-many relationship property of Managed Object as Array of Managed Objects (not NSOrderedSet)

I have a Collection and a Member Managed Object in Core Data.
Collection has a to-many relationship members, that can contain Member objects. Member objects should be uniquely associated to a single Collection.
I want to be able to use the objects in this relationship as Managed Objects for use in populating my Table View Controller, like so:
let members : [Member] = someCollection.members
However, the members property seems to be an NSOrderedSet? object, and not an array of Member.
To this extent, I have tried the following (found on the internet, as I'm very new to Swift) and I am also trying to catch the situation where the relationship field could be nil. It doesn't work for me, and I don't understand why.
let members = someCollection.members?.array as! [Member]
if members != nil {
//Do something with the array
else {
//handle the case that there is no entry in the relationship field
}
I am getting the following error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Please help me understand what I am doing wrong, and if possible, provide a solution.
Since array returns a non-optional you have to optional bind members. If the check succeeds you can safely force unwrap the array. The code checks also if the array is not empty.
if let members = someCollection.members, members.count > 0 {
let memberArray = members.array as! [Member]
//Do something with the array
else {
//handle the case that there is no entry in the relationship field
}

Parse SDK gives: "Error: pointer field owner needs a pointer value" on Facebook login

Ok this error broke our live app and we have no idea of what it is. After a users logs in with Facebook we get this error: Error: pointer field owner needs a pointer value
Any help is appreciated.
When I had this error, it was because I was passing a string instead of an object of the type that I had a pointer to. Try passing the actual object.
Without seeing any code it sounds like you are trying to access the owner field by passing a string instead of explicitly stating and id.
Incorrect:
[query whereKey:#"post"
equalTo:#"1zEcyElZ80"];
Correct:
[query whereKey:#"post"
equalTo:[PFObject objectWithoutDataWithClassName:#"Post" objectId:#"1zEcyElZ80"]];
The problem is that you are parsing STRING, where as it needs to be parsed as a Pointer. Here's a PHP example:
$userId = "123456";
$query = ParseUser::query();
$query->equalTo("objectId", array("__type" => "Pointer", "className" => "_User", "objectId" => $userId));
$result = $query->find();
print_r($result);
It will spit out all the information of the user. I'm sure you can handle the rest. If you are having an issue with iOS, its similar issue just change the syntax.

asp.net webpages: Error when trying to save back to database if DB helper already used

I have an if statement which checks whether or not an entry exists in my DB.
The code correctly checks if an email address is already present, however if the email does not exist i get an error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding
on a null reference at CallSite.Target(Closure , CallSite , Object ) at
ASP._Page__footer_cshtml.Execute()
But if i remove the code that gets the existing email, these lines:
var getrecord = db.QuerySingle("Select Email from Registrations where email = #0", email);
emailexists = getrecord.email;
the data is saved back to the DB with no problems. Not sure if its because i'm using the same Db helper?
try
{
var db = Database.Open("MiniDB");
var getrecord = db.QuerySingle("Select Email from Registrations where email = #0", email);
emailexists = getrecord.email;
if (emailexists != ""){
msg = "This Email address is already registed";
saved = false;
}
else {
var insertCommand = "INSERT INTO Registrations (Email, Name, regDate) VALUES(#0, #1, #2)";
db.Execute(insertCommand, email, name, regDate);
saved = true;
}
}
catch (Exception ex)
{
msg = ex.ToString();
saved = false;
}
One thing I ran into while retrieving (then testing) values within my database in WebMatrix was, depending on the field within the database, that after querying the database and storing the value in a variable, "", null, and System.DBNull.Value were three different things and checking for empty string or simply null was throwing errors for me in much the same way as your example.
Hopefully, I am helping with this (this may not be your issue, but I know it stumped me under similar circumstances, for quite a while, as I had never seen DBNull before).
This DBNull value traveled from the database to the variable I assigned the field value to, as it was a Session Variable and treated like an object. I'm not sure if this DBNull value would still be allowed to be stored in a regular variable (and not converted to regular null). Also, I believe I was using binary data (images) at the time, not sure if that would make a difference either but...
In the end, for my code, when checking to see a field (in the database) was empty, null, etc. I used something like this (only with a session variable instead of your regular variable):
if (emailexists != "" && emailexists != null && emailexists != System.DBNull.Value)
{
//code goes here.
}
I hope this helps!

LINQ Query Result - dynamically get field value from a field name variable

LINQ newbie here
I am trying to get a value of a field - using a fieldName variable.
If I do a watch on row[FieldName] I do get a value - but when I do it on the actual code it will not compile.
string fieldName = "awx_name"
List<awx_property> propertyQry =
(
from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).ToList();
foreach (awx_property row in propertyQry)
{
//THIS DOES NOT WORK
fieldValue = row[fieldName];
}
Thanks in advance. Alternatives would be welcome as well
You keep us guessing what you are trying to do here... You need to specify the types of the objects, so it's easy for us to understand and help. Anyway, I think you are trying to get an object based on the ID. Since you are getting by Id, my guess would be the return value is a single object.
var propertyObj =( from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).SingleOrDefault();
if(propertyObj != null) {
fieldValue = propertyObj.GetType().GetProperty(fieldName).GetValue(propertyObj, null);
}
Of course, you need to add validation to make sure you don't get null or any other error while accessing the property value.
Hope it helps.
What type is fieldValue? What does awx_property look like? This will only work is awx_property is a key/value collection. It its not, you could use reflection instead.
If it is a key/value collection you are probably missing a cast. (row[FieldName].ToString() or something) Also you are missing a semi-colon in the foreach block.

Subsonic 3 Newtonsoft JSON "Self referencing loop Exception"

Hi I been searching for my error but I can't find anything that help me. The problem is this. I been working with Subsonic 3, Newtonsoft Json and the linq way of write so I have this easy query:
var found = from client in newclients.All() where client.Period == "sometext" select client;
string periodoJSON = JsonConvert.SerializeObject(periodoFound); //this get "Self referencing loop Exception"
the problem is when I run this script I get the horrible exception "Self referening loop exception" in the JsonConvert line, subsonic have all the objects without any problem but if I do the following.
var found = from client in newclients.All() where client.Period == "sometext" select new client{client.Name, client.LastName, etc};
string periodoJSON = JsonConvert.SerializeObject(periodoFound);
I get the object serialize with a any problem with all the properties. I'm doing the last way because I have to finish my work but is any other way or solution for this problem, if not I will have to write all the properties every time I want to get a full table properties.
hope any can solve my problem o help me in the path for find a solution....
what I have is a really basic query with linq and I try the three values for JsonSerializerSettings and any work, again I'm working with subsonic 3 this not happend either with subsnoic 2 and I can make it work if I specify one by one the properties of the object in the linq query does any have any clue of what is happend, ANY more help would be great!!! If I put the value of Serialize my page get crazy and in a infinity loop state, if I decide for error simple doesn't work and Ignore nothing happen... some more information about this self referencia loop?
var u = usuario.SingleOrDefault(x => x.TipoUsuario == "A" || x.TipoUsuario == "W");
JsonSerializerSettings setting = new JsonSerializerSettings();
setting.ReferenceLoopHandling = ReferenceLoopHandling.Error; //.Serialize .Ignore
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"usuario", "var usuario=" + JsonConvert.SerializeObject(u, Formatting.None, setting) + ";");
Update ------
I code the following
string jsU = JsonConvert.SerializeObject(u,Formatting.None,new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
and is workign but the only thing wrongs is that in the json object comes all the information about the columns of subsonic 3 and a BIG chunk of text explain it... does any one know how to not SEND this part of the object??
Without knowing more about you object model it is hard to provide a definitive answer, but I would take a look at the ReferenceLoopHandling enum.
You're calling string SerializeObject(object value) on JsonConvert. Try the string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings) method instead. The JsonSerializerSettings settings parameter lets you set a bunch of things, including the ReferenceLoopHandling ReferenceLoopHandling { get; set; } property.
You can try these values:
public enum ReferenceLoopHandling
{
Error,
Ignore,
Serialize
}
Obviously, Error is the default and that's what you're getting. Perhaps one of the others will help.

Resources