how to get the values from Jsonstringify using angular4? - json

I have tried like this from the response:
{success: true, result: {picture: "picture-1529913236852.jpg"}}
I need to get picture only from jsonstringify, Here is my code:
this.http.post('myurl', uploadData)
.subscribe(res =>{
console.log("meee"+JSON.stringify(res));
this.imageResp = JSON.stringify(res.picture) // here i cant take my picture values
});
please Help me.

You do not need JSON.stringify since its already an object,
just use
this.imageResp = res.result.picture;

Related

How to delete all in json server

I am using this json server in my Angular app, to create, fetch, and delete posts.
In the following method, I delete a post with a specified id:
deleteConsumer(post: Post): Observable<Post> {
const url = `${this.apiUrl}/${post.id}`;
return this.httpClient.delete<Post>(url);
}
I looked at the .delete code and searched for something like a .deleteall but could not find it. Is there really no such method that would delete everything?
If there really isn't, then my attempt at doing it myself is not paying off, because what I have done is not working:
deleteConsumers(): Observable<Post> {
let i: number = 0;
this.httpClient.get<Post[]>(this.apiUrl).forEach(
() => {
++i;
const url = `${this.apiUrl}/${i}`;
return this.httpClient.delete<Post>(url);
}
);
}
Obviously, this is wrong in terms of return type, but I cannot figure out what to do... How can I modify the first method, so it would go through all the json objects in my db.json file; meaning iterate through all the existing posts and delete them all?
I did encounter this when using json-server with Vue.js and I realized that there was no special function to delete all at once. I had to work around it.
So, for example in your case, I would first map the posts array to get a new array with only the post ids:
const postsIdsArray = this.posts.map((post) => post.id)
Then, assuming you already have a function to delete one post given the id, I would then execute the function for each of the ids in the array:
postsIdsArray.forEach((id) => this.deletePost(id))
Just combine the two lines in one JavaScript function (in this case I used Vue.js):
deleteAllPosts(){
const postsIdsArray = this.posts.map((post) => post.id)
postsIdsArray.forEach((id) => this.deletePost(id))
}

How to retrieve a name from the data in response in vuejs

I have a issue that i want to get the name from the response of api.
Here is what i have, i have response like this:
capabilities: (55) ["x:list", "y:create", "z:retrieve"]
created_at: "2021-03-09T14:13:18.214999Z"
test: null
description: "test"
What i want to that to have only get the "x:list" from the above
Here is what i am doing is:
this.response.data.data
Please help
I do not know how you process the data, so I cannot give an exact answer. I guess it would be something like this
const { capabilities } = this.response.data
const xList = capabilities.find(item => item === "x:list")
console.log(xList)

Get values from multiple radio group in angular

I want to get selected radio button value then create an object to send to database, I have working example here https://stackblitz.com/edit/angular-1frcbc .I got what i expected object like this
{
lessonCode: 'test',
answers: [
{
questionCode : 'question-1',
optionCode : 'option-2'
},
{
questionCode : 'question-2',
optionCode : 'option-3'
},
]
}
but there is an error ERROR like
Error: Error trying to diff 'Option 2'. Only arrays and iterables are allowed
I believe there is something wrong in here
<input type="radio" id="{{option.order}}" [(ngModel)]="question.options" value="{{option.code}}" name="{{question.order}}">
can someone help me? any thoughts would be helpful
EDIT
i know that question.options can't assign to ngModel if i change it to something like option.order .I'm not getting object like what i expected above, if i change to something like that.
I'm pretty sure i need to change this code
getJsonResult() {
let answer: any;
let data = {
lessonCode: "test",
answers: this.data.questions.map(x => {
return {
questionCode: x.code,
optionCode: x.options
};
})
};
this.submitData = data;
console.log(JSON.stringify(this.submitData))
}
i accidentally change my code to something like this
return {
questionCode: x.code,
status: x.options.code
};
and in the html like this
<input type="radio" id="{{option.order}}" [(ngModel)]="question.options.code" value="{{option.code}}" name="{{question.order}}">
and it works and give me the same result, hope this will help someone

Sequelize raw queries TextRow and getting data out of it

Given this query here,
let output = [];
const sql = `select * from coredb.account LIMIT ${offset},${limit}`;
let data = await sequelize.query(sql, null, {raw: true, type: sequelize.QueryTypes.SELECT});
data.forEach((item) => {
console.log(item['id'], item.id); // <-- output says "undefined, undefined"
});
the data variable is indeed hydrated with the right row data when using console.log to inspect it.
But, when I try to access the individual properties, they only ever come back as undefined. This TextRow object that Sequelize seems to return the result in doesn't seem to want to let me access then explicit rows.
Just curious what i'm missing here, am I missing an option?
I agree, Sequalize raw queries are not intuitive. You don't need the null or raw: true flag. Something like this should work:
let data = await sequelize.query(sql, {type: sequelize.QueryTypes.SELECT});
When I tried this, "data" was an array of two objects, each being the query result. So, the properties can be accessed by using index [0].... e.g.
data[0].forEach((item) => {
console.log(item['id'], item.id); // <-- output says "undefined, undefined"
});
Not yet sure WHY this occurs!
EDIT - it's because .query() should have only two arguments. Changing the call to: sequelize.query(sql, {raw: true, type: sequelize.QueryTypes.SELECT}) resulted in data being a single array (as expected).
Finally I was able to find the solution for it.
You just need to make a new array and push data into it by finding bases on key name like this:
suppose we have data in students object:
let finalArray = new Array();
for (var k in students ) {
finalArray.push(students[k])
}
console.log(finalArray) // Normal JSON array object :)
m.sequelize.query(sql, {
model,
mapToModel: true
})
.then(model => res.status(200).send(model))
.catch(error => res.status(400).send(error.toString())
})

Laravel eloquent add to JSON

To begin with, sorry for my bad English. I have column:
$table->json('images')->nullable();
Table is called albums. In images I would like to store an array with image names, but how can I every time add to that json? My code right now:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$album->update([
'images' => $request->image->name <= tried to do something.. My uploader everytime calls this function, so I need to get current IMAGES column value, and add new. Like ['first image name', 'second image name'], after update this must be ['first image name', 'second image name', 'third image name'] not just ['third image name']
]);
$album->save();
I need to do like laravel increment function, but which works only with int looks like. How can I do that? Thanks in advance!
I think its not possible to directly change the JSON as your images data would return as a String.
First get the JSON as a string, then update the Object and update the JSON.
So something like:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$images = json_decode($album->images);
array_push($images, $request->image->name);
$album->update(['images' => $images]);
$album->save();
Note: I didn't check this code, but this should give you an idea of how to handle this
Good luck
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$arra = json_decode($album->images);
$arra[] = $request->image->name;
$album->update([
'images' => $arra
]);
$album->save();
I hope help you