I have an elasticsearch database that I access through a node.js client. I can parse the JSON from elasticsearch to print out the hitsArray which looks like below:
[ { _index: 'parties',
_type: 'suppliers',
_id: 'AV0uELknL82XeGsCOZ-i',
_score: 1,
_source: { name: 'Jabil', address: [Object], rating: 4.2 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_yC3L82XeGsCOZ-f',
_score: 1,
_source: { name: 'Apple', address: [Object], rating: 4.9 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_glkL82XeGsCOZ-d',
_score: 1,
_source: { name: 'Flextronics', address: [Object], rating: 4.5 } },
{ _index: 'parties',
_type: 'suppliers',
_id: 'AV0t_ox7L82XeGsCOZ-e',
_score: 1,
_source: { name: 'FlashMob', address: [Object], rating: 3.5 } } ]
Now, I want to parse the _source field and print out the name field
client.search(searchParams).then(function (resp) {
return Promise.all(resp.hits.hits)
}).then(function(hitsArray){
hitsArray.forEach(function(value){
JSONObject first = hitsArray.getJSONObject(value);
JSONObject source = first.getJSONObject("_source");
String name = source.getString("name");
console.log(name);
});
});
But I am getting error
JSONObject first = hitsArray.getJSONObject(value);
^^^^^
SyntaxError: Unexpected identifier
What am I missing? any suggestion?
JavaScript has no explicit type definitions like that. It looks like those 3 lines may be Java code and not JavaScript code.
As far as getting the actual JavaScript value goes, it appears to already be an object. Just access it with value._source (and value._source.name).
Related
Hi I use the opensea api to create an offer but all it returns is a 400 Error with the message "Asset does not exist". Even though I use the same API before to retrieve the asset. I use the seaport SDK to create the signature and it all works fine
const { executeAllActions } = await seaport.createOrder(
{
offer: [
{
amount: basePrice.toString(),
token: WETH,
}
],
consideration: [item, ...considerationFeeItems],
endTime: endTime.toString(),
zone: DEFAULT_ZONE_BY_NETWORK[opensea.Network.Main],
restrictedByZone: true,
allowPartialFills: false,
conduitKey: CROSS_CHAIN_DEFAULT_CONDUIT_KEY,
counter: 0
},
ourAddress
);
const order = await executeAllActions();
But as soon as I send it to that endpoint https://docs.opensea.io/v2.0/reference/create-an-offer
it gives me back an error.
The order object returned from executeAllActions looks like following:
{
parameters: {
offerer: '0x...',
zone: '0x004c00500000ad104d7dbd00e3ae0a5c00560c00',
zoneHash: '0x3000000000000000000000000000000000000000000000000000000000000000',
startTime: '1660557986',
endTime: '1660644385',
orderType: 2,
offer: [ [Object] ],
consideration: [ [Object], [Object], [Object] ],
totalOriginalConsiderationItems: 3,
salt: '0xc532bab0fd9ae9529b4d8cfc9fc2f02e',
conduitKey: '0x0000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f0000',
counter: 0
},
signature: '0x....'
}
What is going wrong?
Brand new to react-native and typescript!
I'm have a bit of trouble extracting JSON response. I was to extract the response and put it into a class as shown below.
Here is the request code
let notifications: INotification[]
notifications = (await Requests.GET('notification/user/test-user-1', accessToken));
Here is the class
export interface INotification {
id: string;
senderId: string;
receiverId: string;
text: string;
isSeen: boolean;
type: string;
timestamp: string;
}
Here is the Postman response
{
"notifications": [
{
"pk": "user-1",
"sk": "notification1234",
"entity": "notification",
"id": "id number",
"senderId": "test-user-2",
"receiverId": "test-user-1",
"text": "Test notifications",
"isSeen": false,
"type": 2
}
]
}
Here is response from the console
{ notifications:
[ { pk: 'user#test-user-1',
sk: 'notification1234',
entity: 'notification',
id: 'id number',
senderId: 'test-user-2',
receiverId: 'test-user-1',
text: 'Test notifications',
isSeen: false,
type: 2 } ]
}
I want to be able to write out:
console.log("TEXT: ",notifications[0].text )
And get the response of : "Text: Test notifications"
Any help welcome!
the data is in an array you need to pass the array first
console.log("TEXT: ", notifications[0].text);
This is my req.body json data from angularjs controller:
{
phoneno: [
{ id: 1, gsm: '123457801', firstName: 'Mohamed', lastName: 'Sameer'},
{ id: 2, gsm: '123450987', firstName: 'Hameed', lastName: 'Basha' }
],
sender: 'ifelse',
message: 'Hello Test'
}
i want to get gsm values from req.body
I want to make the above structure into this type:
[{phoneno:123457801;sender:'ifelse';message:'Hello Test'},{phoneno:123450987;sender:ifelse;message:'Hello Test'}]
then only i am able to insert in mysql.
I think it will helpful to you!...
var body={
phoneno: [
{ id: 1, gsm: '123457801', firstName: 'Mohamed', lastName: 'Sameer'},
{ id: 2, gsm: '123450987', firstName: 'Hameed', lastName: 'Basha' }
],
sender: 'ifelse',
message: 'Hello Test'
};
var new_req_body=[];
for(var i=0;i<body.phoneno.length;i++){
var new_arr={
sender:body.sender,
message:body.message,
phoneno:body.phoneno[i].gsm
};
new_req_body.push(new_arr);
}
console.log(new_req_body);
I'm making a small website with i18n. When starting I used local json files, but after switching to mongodb I experience an error I don't understand. A good explanation is highly appreciated.
The error I get is this:
TypeError: Method Uint8Array.length called on incompatible receiver [object Object]
I structure i18n data with "da" and "en" properties. I then use a method for filtering relevant language.
Here are examples of my data, both json and corresponding mongoose result. Both copied from terminal (printed with console.log):
json
[ { lang: { da: 'Dansk', en: 'Danish' }, rating: 5 },
{ lang: { da: 'Engelsk', en: 'English' }, rating: 5 },
{ lang: { da: 'Tysk', en: 'German' }, rating: 5 } ]
mongoose
[ { _id: 57e2561369e4bc0a8ca6c630,
lang: { da: 'Dansk', en: 'Danish' },
rating: 5,
id: '57e2561369e4bc0a8ca6c630' },
{ _id: 57e2561369e4bc0a8ca6c631,
lang: { da: 'Engelsk', en: 'English' },
rating: 5,
id: '57e2561369e4bc0a8ca6c631' },
{ _id: 57e2561369e4bc0a8ca6c632,
lang: { da: 'Tysk', en: 'German' },
rating: 5,
id: '57e2561369e4bc0a8ca6c632' } ]
filterLanguage method
var traverse = require('traverse');
var filterLanguage = function(language, obj) {
return traverse(obj).map(function (item) {
if (this.key === language) {
this.parent.update(item);
}
});
};
So filterLanguage('da', languages); should return something like:
[ { lang: 'Dansk', rating: 5 },
{ lang: 'Engelsk', rating: 5 },
{ lang: 'Tysk', rating: 5 } ]
filterLanguage() works on a local, valid JSON file but not on the Mongoose result set...
I tried JSON.stringify(obj). I also tried setting toObject() method in Mongoose models but no luck.
What am I doing wrong and how can I fix it?
--------- EDIT ---------
Model
var mongoose = require('mongoose');
var languageSchema = mongoose.Schema({
local: {
lang: { da: String, en: String },
rating: Number
}
});
module.exports = mongoose.model('Language', languageSchema);
Query
language.find({}, function(err, results) {
var obj = filterLanguage(lang, results.languages);
console.log(obj);
});
I tried setting toObject on the schema like this:
languageSchema.set('toObject', { virtuals: true });
The error you get is related to traverse not being able to handle ObjectId instances in your results array.
Since it looks like you don't use _id or id, the easiest way to fix this is to exclude those properties from the result documents:
language.find({}, '-_id -id', function(err, results) { ... })
(-id is probably superfluous, because it's a virtual that depends on _id)
I have the following Json -
Property {
id: 122334,
source:
[ id : 123,
address:
{
city: "little rock",
state: "Arkansas",
country: "USA"
},
unit:
{
id: 222,
name: "The wall",
count: 2
}, ]
[ {id: 8889,
address:
{
city: "milka",
state: "Arkansas",
country: "USA"
},
unit:
{
id: 555,
name: "The watt",
count: 3
},
},
]
}
I am parsing it the following way -
string data = client.DownloadString(URL);
JToken token = JObject.Parse(data);
if (!string.IsNullOrEmpty(Convert.ToString(token["property"].Children())))
{
token["property"].Children().ToList().ForEach(child =>
{
string GetID = Convert.ToString(child["source"]["unit"]["id"]);
if (GetID == id)
{
//move rest of the code here
}
else
{
}
});
}
But I get the execption - cannot access child value on newtonsoft.json.linq.jproperty.
at the line -
string GetID = Convert.ToString(child["source"]["unit"]["id"]);
What am I doing wrong?
enter code here