I do not know how to manipulate a formanray
I have with the select input:
example stackblitz
I want the output json like this:
json
{
"comentario": "kkkkkkkkkkkk",
"respuestas": [ { "idusuario": "", "numero": "","iduser":"" } ]
}
I get iduser from localstorage with that I have no problem
I found the how to do it
Update I make this little example but there a bug with select
https://stackblitz.com/edit/angular-kj2ahg?file=src%2Fapp%2Fapp.component.html
Your form module (what you get from Formulario.value) does not need to match your data model.
Consider modeling your data as you need it coming from or getting saved to your data store. So in your example, with your iduser.
Then just map the data from your form data structure (Formulario.value) to your data structure.
Make sense?
For example, my data structure I want to send to my server could look like this:
export interface Product {
id: number;
productName: string;
productCode: string;
iduser: number;
}
I can copy the data from the form to this data structure like this:
const product = { ...this.myForm.value };
Then product contains all of the form fields. But, like in your example, the iduser is not on the form. So I can then use:
product.iduser = myUsersId;
To set the id. I then have a data structure I can pass to my backend.
Related
So I am coming from a background of C# where I can do things in a dynamic and reflective way and I am trying to apply that to a TypeScript class I am working on writing.
Some background, I am converting an application to a web app and the backend developer doesn't want to change the backend at all to accommodate Json very well. So he is going to be sending me back Json that looks like so:
{
Columns: [
{
"ColumnName": "ClientPK",
"Label": "Client",
"DataType": "int",
"Length": 0,
"AllowNull": true,
"Format": "",
"IsReadOnly": true,
"IsDateOnly": null
}
],
Rows:[
0
]
}
I am looking to write an Angular class that extends Response that will have a special method called JsonMinimal which will understand this data and return an object for me.
import { Response } from "#angular/http";
export class ServerSource
{
SourceName: string;
MoreItems: boolean;
Error: string;
ExtendedProperties: ExtendedProperty[];
Columns: Column[];
}
export class ServerSourceResponse extends Response
{
JsonMinimal() : any
{
return null; //Something that will be a blank any type that when returned I can perform `object as FinalObject` syntax
}
}
I know StackOverflow isn't for asking for complete solutions to problems so I am only asking what is one example taking this example data and creating a dynamic response that TypeScript isn't going to yell at me for. I don't know what to do here, this developer has thousands of server-side methods and all of them return strings, in the form of a JSON or XML output. I am basically looking for a way to take his column data and combine it with the proper row data and then have a bigger object that holds a bunch of these combined object.
A usage case here after that data has been mapped to a basic object would be something like this.
Example:
var data = result.JsonMinimal() as LoginResponse; <-- Which will map to this object correctly if all the data is there in a base object.
var pk = data.ClientPK.Value;
I'm not exactly sure I understand, but you may want to try a simple approach first. Angular's http get method returns an observable that can automatically map the response to an object or an array of objects. It is also powerful enough to perform some custom mapping/transformation. You may want to look at that first.
Here is an example:
getProducts(): Observable<IProduct[]> {
return this._http.get(this._productUrl)
.map((response: Response) => <IProduct[]> response.json())
.do(data => console.log('All: ' + JSON.stringify(data)))
.catch(this.handleError);
}
Here I'm mapping a json response to an array of Product objects I've defined with an IProduct interface. Since this is just a "lambda" type function, I could add any amount of code here to transform data.
I would like to emit couchbase data in the following format :
rows: [
{
id: "UniqueID",
key: "UniqueKey",
doc: {
meta: {
id: "UniqueID"
},
json: {
//ACTUAL DOCUMENT HERE
}
}
}
,
.... Second document and so on
When I try to create a view :
function (doc, meta) {
emit(meta.id, doc);
}
It emits the data in the following fashion :
total_rows: 55, -- DO NOT NEED THIS
rows: [
{
id: "UniqueID",
key: "UniqueKey",
value: {
//ACTUAL DOCUMENT HERE
}
},
.... Second document and so on
How do I modify the view to output the exact same schema as mentioned above ?
You don't. View response follows a defined format that tools like the SDKs rely on for parsing.
Also it is generally not a good idea to emit the whole document: since the value of the view get stored in the secondary index, you are basically duplicating all your data in the key/value store and the view index...
View responses always include the document ID for a particular row, so you can always get the document by performing a key/value GET. The SDKs can also abstract that away in their API (eg. in Java there's a document() method on each row object).
After doing a POST to Firebase, I have this returned from Firebase
{ "name": "-KBfn7iFNIxSuCL9B-g5" }
How do I use Typescript to read the response returned from the POST request? Only the value -KBfn7iFNIxSuCL9B-g5 is need for routing. My existing incomplete code looks like this:
addHeroGotoHeroDetail() {
let heroId: string;
this._firebaseService.postHero()
.subscribe(response => {
//do something to make the heroId = -KBfn7iFNIxSuCL9B-g5
});
this._router.navigate(['hero-detail', {id: heroId}]);
}
The goal is to have a button in the homepage where user can click on it to create a new hero Id in Firebase then redirect it to the newly created hero detail page, where he can do more detail updating.
Also after doing a GET from Firebase, I have this returned from Firebase
{ "-KBfn-cw7wpfxfGqbAs8": { "created": 1456728705596, "hero": "Hero 1", "...": "..."} }
Should I create an interface for the hero JSON returned by Firebase?
If yes for question 2, how should the interface look?
export interface X {
id: string; //id defined by Firebase e.g. -KBfn-cw7wpfxfGqbAs8
hero: string; //name of the hero e.g. Hero 1
created: number; // The time when the hero was created
}
If yes for question 2, how do I use Typescript to parse the JSON object to the interface? Code sample would be very much appreciated.
The goal is to display the hero detail to the user, and allow the user to add more details to the hero such as Date of Birth, Gender and etc... and then update Firebase with these changes.
1. See below.
2. Yes, you should create an interface that matches the return type from Firebase.
3. Based on your example JSON, this is how I would structure the return type's interface:
export interface FirebaseResponse {
[id: string]: {
created: number;
hero: string;
}
}
4. To parse the response from Firebase into a strongly-typed TypeScript object, do something like this:
let firebaseResponse = <FirebaseResponse>JSON.parse(response);
You can then return only the ID, if you wish, by doing something like this:
// note: this is making some assumptions about your response type.
// You may need a more sophisticated way of returning the ID depending
// on the structure of the response - for example, if the object has
// more than one key.
return Object.keys(firebaseResponse)[0];
I have a database with lots of document and i am using field type to define it as a table. I want to populate angularjs ui-grid with the JSON data coming in value. So i have created a view:
function(doc) {
if(doc.type === 'userTable'){
emit(doc._id, {userName:doc.userName,fName:doc.fName});
}
}
When i hit the url http://127.0.0.1:5984/rpt_db/_design/Dsgn_userjson/_view/Vw_userjson/ it gives me :
{"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}
Now in my client code i need to do a loop and make data as JSON.
result.rows.forEach(function(item) {
var temp = { "id": item.value.userName, usrName: item.value.fName};
console.log(temp);
//Update data of grid using temp variable.
});
Is there an easy way so that i can directly use what's coming as part of couchdb JSON output which can be used as is in angular ui-grid.
You can refer column values using json dot notation in the columnDefs. Since your actual values are inside "value". you can create column definition like this,
$scope.myData = {"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}
$scope.gridOptions = {
data : $scope.myData.rows,
columnDefs : [
{name:"id",field:"value.userName"},
{name:"usrName",field:"value.fName"}
]
};
Here's a working plnkr.
http://plnkr.co/edit/juLZeT6I0LOlek4QnMSP?p=preview
Im using Symfony2 with FOSRestBundle on the server side, and EmberJS as client. im reciving the data from the server like this:
{
customers:[
{
id:3,
name:"Joue",
currency:{
id:5,
iso_code:"BDT"
}
}
]
}
I populate a select with a 2nd server call, where i get all the currencies.
At the moment im sending back the data (PUT - update) like this:
{
customers:[
{
id:3,
name:"Joue",
currency: 2
}
]
}
and in the controller i look up for the currency with the given id, and i save it.
$currency = $this->getDoctrine()
->getRepository('ApiBundle:Currency')
->findOneBy(array('id' => $req['customer']['currency']));
$partner->setCurrency($currency);
my question is there a way to save a request if i send it back with embedded JSON? eg:
{
customers:[
{
id:3,
name:"Joue",
currency:{
id:2,
iso_code:"XCT"
}
}
]
}
or it is fine to look up for it and handle in the controller.
My opinion:
I prefer querying the database to get the right currency (make sure nobody tries to inject some false data for instance a currency with iso_code "IAJSAIJD"). This way I'm sure everything is fine on $em::flush().
On the other hand, you could use the #ParamConverter from FOSRestBundle and send a full json. In you Customer entity you can specify a way to deserialize your currency association (See #JMSSerializer annotations, if you are using it).
class Customer
{
//...
/*
* #ORM\OneTo..
* #JMS\Type("My\Entity\Currency")
*/
$currency;
}
But this solution assumes that you allow your code to manage entities' ids, because you need to tell Doctrine that your currency already exists and this is the one with the id X. If you have a cascade persist on $currency, for instance, and you forget to $em::merge() before $em::flush(), you will end up with a new currency in your database.
That being said, I might be wrong, I'm no expert.