Reputation: 707
I have a form that is used to create a object of type Equipment, with the properties, name and id. When I submit the form, I get the data. Into the Equipment object.
var result: Equipment = this.addEquipment.value;
then I create a instance of CreateEquipment, where I would like the values from Equipment to be inserted.
var createEquipment: CreateEquipment = new CreateEquipment();
I then try to add the name from result.
createEquipment.name = result.name;
This is where it fails(the value is not stored) However when I do this
createEquipment.name = "ASDASDASD";
it works.
Is there something obvious I am missing?
Picture of the console.log(result) and console.log(createEquipment) after the name should have been defined.
Upvotes: 2
Views: 637
Reputation: 143399
This doesn't contain enough info to determine what the issue is, but whenever you use an explicit cast you're telling the compiler what the Type is, it doesn't know at runtime if that assertion is true, its up to you to ensure that the type you tell it to treat it as is the Type it receives at runtime.
When you run into an issue like this, log the actual value so you can confirm it matches the type/shape you're expecting, i.e:
var result: Equipment = this.addEquipment.value;
console.log(result);
You never need to specify the type when creating a new instance, it's implied and unnecessary duplication, just do:
var createEquipment = new CreateEquipment();
If this is the issue, log to see what you're actually assigning:
console.log(result.name, result);
createEquipment.name = result.name;
Your question suggests that result.name
doesn't contain what you think it does.
Looking at the updated screenshot indicates the issue is that the runtime JavaScript object is using PascalCase but the Type assumes camelCase. If you're using ServiceStack with .NET Core it should be using camelCase by default unless you've overridden it somehow, e.g with JsConfig.*
or are otherwise forcing it to use PascalCase.
If you're not using .NET Core you can force camelCase with:
SetConfig(new HostConfig {
UseCamelCase = true
});
Upvotes: 2