I have an api endpoint form where I am getting data like below. How i will access the values title, short_title etc.
blog: {
paginations: true,
isLoading: false,
particularBlog: [],
count: 13,
next: 'http://127.0.0.1:8000/api/blog/all-blog/?page=2',
previous: null,
results: [
{
id: 47,
user: 1,
title: 'adasd',
short_title: 'asd',
publish: '2019-09-16',
slug: 'adasd',
comments_count: 0,
likes_count: 0
},
{
id: 46,
user: 1,
title: 'adasda',
short_title: 'asdas',
publish: '2019-09-16',
slug: 'adasda',
comments_count: 0,
likes_count: 0
}
]
},
what i have done is
<div>{
this.props.blog && Object.keys(this.props.blog).map((key) => {
return <p>{this.props.blog.results[key]}</p>
})
}</div>
but it is giving error stating paginations' of undefined. Can someone please pointout what i am doing wrong here?
Where is what is happening
Object.keys(this.props.blog).map((key) => { is getting the keys of this.props.blog and this.props.blog.results[key] is trying to access the properties of results with the keys of blog.
What you should do is have another .map with Object.keys of this.props.blog.results
OR
What I think you are trying to do is list all properties on the this.props.blog.results array, so here is what you can do
this.props.blog && this.props.blog.results && this.props.blog.results.map(result=> <p>{result.short_title}</p>
You do .map on the results array and display short_title.
blog.results.map(el => (<div>{el.short_title}</div>))
Related
There's about million questions (and answers) out there on this topic, but none of them are doing what I need to do. I have a JSON object where the value for each key is an object. I want to convert this to an array and maintain the top level keys.
{
"someKey1": {
active: true,
name: "foo"
},
"someKey2": {
active: false,
name: "bar"
}
}
If I use Object.keys() I get the top level keys, but not their values. If I use Object.values() I get an array with all the values, but not their keys. I'm trying to use keys and map, but am only getting the values returned:
const data = {
"someKey1": {
active: true,
name: "foo"
},
"someKey2": {
active: false,
name: "bar"
}
}
const items = Object.keys(data).map(function(key) {
return data[key];
});
// returns [{active: true, name: foo},{active: false, name: bar}]
Is there a way to get both? I want to get an array I can iterate over that looks something like this:
[{
key: someKey1,
active: true,
name: "foo"
},
{
key: someKey2,
active: true,
name: "foo"
}]
OR
[
"someKey1": {
active: true,
name: "foo"
},
"someKey2": {
active: false,
name: "bar"
}
]
I think you are going in the right direction, if you want to add the "key" property you have to map the properties manually and for the second option since you don't need the "key" property it can be done a little bit more elegantly:
For the first option:
Object.keys(data).map(v => ({
key: v,
...data[v]
}));
For the second option even simpler:
Object.keys(data).map(v => ({[v]: {...data[v]}}))
You can easily map your data to a new object:
Object.keys(data).map(key => ({ ...data[key], "key": key }));
This may be a non-issue however I can't help but feel like there is a better way to do what I am trying to achieve.
I am writing an API in Express with my data stored in a neo4j database.
I am using the official neo4j-driver to interface with neo4j which is running locally. When I run a query eg :
session
.run(`MATCH (foo:FamilyMember)-[:HAS_SISTER]->(sister:FamilyMember)
WHERE foo.firstName = 'bar'
RETURN sister.firstName AS Name, sister.lastName AS Surname
`)
.then(res => {
console.log(res);
})
Neo4j returns a response object with a lot of information about the request:
{
records: [
Record {
keys: [Array],
length: 2,
_fields: [Array],
_fieldLookup: [Object]
},
Record {
keys: [Array],
length: 2,
_fields: [Array],
_fieldLookup: [Object]
},
Record {
keys: [Array],
length: 2,
_fields: [Array],
_fieldLookup: [Object]
},
Record {
keys: [Array],
length: 2,
_fields: [Array],
_fieldLookup: [Object]
}
],
summary: ResultSummary {
query: {
text: 'MATCH (foo:FamilyMember)-[:HAS_SISTER]->(sister:FamilyMember)\n' +
" WHERE foo.firstName = 'bar'\n" +
' RETURN sister.firstName AS Name, sister.lastName AS Surname\n' +
' ',
parameters: {}
},
queryType: 'r',
counters: QueryStatistics { _stats: [Object], _systemUpdates: 0 },
updateStatistics: QueryStatistics { _stats: [Object], _systemUpdates: 0 },
plan: false,
profile: false,
notifications: [],
server: ServerInfo {
address: 'localhost:7687',
version: 'Neo4j/4.2.1',
protocolVersion: 4.2
},
resultConsumedAfter: Integer { low: 0, high: 0 },
resultAvailableAfter: Integer { low: 4, high: 0 },
database: { name: 'neo4j' }
}
}
This is a pain when really I what I want is either the actual response data as an array of objects or an error message if something fails.
I wrote this parse method to generate an array of Javascript object's with the data returned from the query:
function parseNeo4jResponseJSON(res) {
return results = res.records.reduce( (array, currentRecord) => {
const record = currentRecord.keys.reduce( (obj, key, index) => {
obj[key] = currentRecord._fields[index]
return obj
}, {})
array.push(record);
return array;
},[])
};
This works and now when I console log the query response through my parser I get it in the format I want eg:
[
{ Name: 'foo', Surname: 'bar' },
{ Name: 'foo2', Surname: 'bar2' },
...
]
Is this approach going to cause me problems down the line? Is there a better way to get a javascript object from the response? I am pretty new to neo4j. Apologies if the answer is obvious.
Based on the existing examples, what about:
session
.readTransaction((tx) =>
tx.run(`MATCH (foo:FamilyMember)-[:HAS_SISTER]->(sister:FamilyMember)
WHERE foo.firstName = 'bar'
RETURN sister.firstName AS Name, sister.lastName AS Surname`)
)
.then(results => results.records.map((record) => {
return {
Name: record.get('Name'),
Surname: record.get('Surname')
}
})
You could keep session.run, but the session.{read,write}Transaction variants are usually recommended because they work in every environment (where session.run may sometimes fail in a cluster environment).
Please also make sure to use a dictionary of parameters (2nd argument of tx.run) instead of using string interpolation, if your query needs to be parameterized. If the value of foo.firstName comes from a variable (let's say someFirstName), the tx.run would become:
tx.run("MATCH (foo:FamilyMember)-[:HAS_SISTER]->(sister:FamilyMember)
WHERE foo.firstName = $firstName
RETURN sister.firstName AS Name, sister.lastName AS Surname",
{firstName: someFirstName})
I needed to convert some data in my reducer from an array to object. So I used Object.keys to do so. But I started to get this error that it cannot convert undefined.
After troubleshooting for quite a bit I have reduced the code in my reducer to its simplest form as below and I'm still seeing the same error. Can someone please advise what is the issue here? Ofcourse now that I have removed Object.keys I'm seeing a slightly different error but the main issue seems to be the same. The object in the state is undefined.
Thank you!
Just to add something when I do a console.log(state.collections["hats"]);
right before the switch statement, it shows the data as expected i.e the object/value for the "hats" key
TypeError: Cannot read property 'hats' of undefined
My simplified Reducer code:
import SHOP_DATA from '../../data/shop.data.js';
const INITIAL_STATE = {
collections: {
hats:{
id: 1,
title: 'Hats',
routeName: 'hats',
items: [
{
id: 1,
name: 'Brown Brim',
price: 25
},
{
id: 2,
name: 'Blue Beanie',
price: 18
}
]
},
sneakers: {
id: 2,
title: 'Sneakers',
routeName: 'sneakers',
items: [
{
id: 10,
name: 'Adidas NMD',
price: 220
},
{
id: 11,
name: 'Adidas Yeezy',
price: 280
}
]
}
}
}
const collectionsReducer = (state= INITIAL_STATE, action) => {
switch(action.type) {
default:
return ["hats", "sneakers"].map(key => state.collections[key]);
}
}
export default collectionsReducer;
Change the reducer logic like below then it will work
switch(action.type) {
default:
return ["hats", "sneakers"].map(key =>INITIAL_STATE['collections'][key])
}
Thank you
there is no key named sneakers, thats why it is returning undefined
I am using some assignment destructuring in my MongoDB/Node backend in order to handle some post-processing. I'm just trying to understand how this destructuring works, and if, in the case of an array of multiple elements and nested arrays, if I can input the element I want to target.
Take for instance this code:
services: [
,
{
history: [...preSaveData]
}
]
} = preSaveDocObj;
My assumption is that the "," in "services" for the above code will default to looking at the first element in the array. Correct?
Now, if I have a document structure that looks like this (see below), and I know I want to target the "services" element where "service" is equal to "typeTwo", how would I do that?:
{
_id: 4d39fe8b23dac43194a7f571,
name: {
first: "Jane",
last: "Smith"
}
services: [
{
service: "typeOne",
history: [
{ _id: 121,
completed: true,
title: "rookie"
},
{ _id: 122,
completed: false,
title: "novice"
}
]
},
{
service: "typeTwo",
history: [
{ _id: 135,
completed: true,
title: "rookie"
},
{ _id: 136,
completed: false,
title: "novice"
}
]
}
]
}
How can I edit this code (see below) to specifically target the "services" array where "service" is equal to "typeTwo"?
services: [
,
{
history: [...preSaveData]
}
]
} = preSaveDocObj;
Don't overdestructure, just find:
const { history: [...preSavedData] } = doc.services.find(it => it.serice === "typeTwo");
I need to find the index of the mongoose objectID in an array like this:
[ { _id: 58676b0a27b3782b92066ab6, score: 0 },
{ _id: 58676aca27b3782b92066ab4, score: 3 },
{ _id: 58676aef27b3782b92066ab5, score: 0 }]
The model I am using to compare is a mongoose schema with the following data:
{_id: 5868d41d27b3782b92066ac5,
updatedAt: 2017-01-01T21:38:30.070Z,
createdAt: 2017-01-01T10:04:13.413Z,
recurrence: 'once only',
end: 2017-01-02T00:00:00.000Z,
title: 'Go to bed without fuss / coming down',
_user: 58676aca27b3782b92066ab4,
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false }
I am trying to find the index of the model._user in the array above using the following:
var isIndex = individualScores.map(function(is) {return is._id; }).indexOf(taskList[i]._user);
Where individualScores is the original array and taskList[i] is the task model. However, this always returns -1. It never finds the correct _id in the array.
I guess your problem is related to how _id are returned by your query
If you get _id as String, your code should work, check the snippet below
But if instead, you get ObjectsIds, you have to cast them to String first
var individualScores = [
{ _id: "58676b0a27b3782b92066ab6", score: 0 },
{ _id: "58676aca27b3782b92066ab4", score: 3 },
{ _id: "58676aef27b3782b92066ab5", score: 0 }
]
var task = {
_id: "5868d41d27b3782b92066ac5",
updatedAt: new Date("2017-01-01T21:38:30.070Z"),
createdAt: new Date("2017-01-01T10:04:13.413Z"),
recurrence: 'once only',
end: new Date("2017-01-02T00:00:00.000Z"),
title: 'Go to bed without fuss / coming down',
_user: "58676aca27b3782b92066ab4",
__v: 0,
includeInCalc: true,
result: { money: 0, points: 4 },
active: false,
pocketmoney: 0,
goals: [],
pointsawarded: { poorly: 2, ok: 3, well: 4 },
blankUser: false
}
var isIndex = individualScores.map(
function(is) {
return is._id;
})
.indexOf(task._user);
console.log(isIndex)
I think your process should working well that you are using just only need to convert 'objectID' to String to compare. Convert using .toString() for both of _id and _user.
like bellow:
var isIndex = individualScores.map(function(is) {
return is._id.toString();
}).indexOf(taskList[i]._user.toString());