Accessing [Object] from POST response - javascript

How do I access the [Object] and return the value for each object to the console?
Response:
[ 'qpxExpress#tripsSearch',
{ kind: 'qpxexpress#tripOptions',
requestId: 'EWwQPoK0fma2Wgz1x0PNGc',
data:
{ kind: 'qpxexpress#data',
airport: [Object],
city: [Object],
aircraft: [Object],
tax: [Object],
carrier: [Object] },
tripOption: [ [Object] ] } ]
Request:
const options = {
method: 'POST',
uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?&key=XXXXXXXXXXXXXXXXX',
body: {
"request": {
"passengers": {
"adultCount": "1"
},
"slice": [
{
"origin": "SFO",
"destination": "LAX",
"date": "2017-09-19"
}
],
"solutions": "1"
}
},
json: true
}

Just needed to call on the nested object.
Reading the api documentation helped.
Updated code that returns the total price:
request(options)
.then(function (response) {
console.log(response.trips.tripOption[0].saleTotal)
})
.catch(function (err) {
console.log(err)
})

Related

Issue while updating nested data createEntityAdapter RTK query

I am working on the chat that is utilising RTK Query with entity adapters. We created nested normalised createEntityAdapter . I want to update the message key from onCacheEntryAdded, but not able to update nested object. I'm attaching the query endpoint code along with sample response.
RTK Query Endpoint
endpoints: builder => ({
getMessages: builder.query({
query: ({ orderId, userId, channel }) => {
return {
url: `/messages?orderId=${orderId}&userId=${userId}`,
method: 'GET',
}
},
transformResponse(response, meta, arg) {
return messagesAdapter.setAll(messagesAdapter.getInitialState(), [
{
id: `${arg.orderId}-${arg.userId}`,
isTyping: false,
channel: arg.channel,
orderId: arg.orderId,
messages: messagesAdapter.addMany(
messagesAdapter.getInitialState(),
response,
),
},
])
},
async onCacheEntryAdded(
arg,
{ updateCachedData, cacheDataLoaded, cacheEntryRemoved, cacheData },
) {
try {
await cacheDataLoaded
updateCachedData(draft => {
messagesAdapter.updateOne(draft, {
id: '62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72',
changes: {
messages: messagesAdapter.updateOne(
messagesAdapter.getInitialState(),
{
id: '62da7221bc0eb3a259fc2256',
changes: {
message: 'updated message',
},
},
),
},
})
})
} catch {
// no-op in case `cacheEntryRemoved` resolves before `cacheDataLoaded`,
// in which case `cacheDataLoaded` will throw
}
await cacheEntryRemoved
},
providesTags: (result, error, arg) => {
console.log('providesTags', result, error, arg)
return ['chat']
},
}),
}),
Sample response
{
"ids": [
"62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72"
],
"entities": {
"62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72": {
"id": "62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72",
"isTyping": false,
"channel": "62d7d9266599a9f0c3bcdaba",
"orderId": "62d529f60be7549be1f53df3",
"messages": {
"ids": [
"62da7221bc0eb3a259fc2256",
"62da724dbc0eb3a259fc2260",
"62da75559f7a23f89a0958d4",
"62da94e59f7a23f89a0958ed"
],
"entities": {
"62da7221bc0eb3a259fc2256": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "dsds",
"sentAt": "2022-07-22T09:47:13.000Z",
"id": "62da7221bc0eb3a259fc2256"
},
"62da724dbc0eb3a259fc2260": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "hello nm",
"sentAt": "2022-07-22T09:47:57.000Z",
"id": "62da724dbc0eb3a259fc2260"
},
"62da75559f7a23f89a0958d4": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "b",
"sentAt": "2022-07-22T10:00:53.000Z",
"id": "62da75559f7a23f89a0958d4"
},
"62da94e59f7a23f89a0958ed": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "hello there",
"sentAt": "2022-07-22T12:15:31.000Z",
"id": "62da94e59f7a23f89a0958ed"
}
}
}
}
}
}

Fetching information from Roblox API about a game

I am trying to fetch information from: https://games.roblox.com/v1/games?universeIds=3759152694
But having it try to response with "response.data[0]" (I use jsonpathfinder) it gives me undefined. When I try to do "response.data[0].id" it says that id is not defined. I have not messed with apis in quite a while so I may be just missing something here. Any help is appreciated!
const axios = require("axios");
module.exports = {
name: "info",
execute: async (bot, message, args) => {
test = {
data: [
{
id: 3759152694,
rootPlaceId: 10269801695,
name: "unnamed sim",
description: "soon",
sourceName: "unnamed sim",
sourceDescription: "soon",
creator: { id: 3593901883, name: "Jack1286401", type: "User", isRNVAccount: false, hasVerifiedBadge: false },
price: null,
allowedGearGenres: ["All"],
allowedGearCategories: [],
isGenreEnforced: false,
copyingAllowed: false,
playing: 0,
visits: 0,
maxPlayers: 6,
created: "2022-07-18T14:21:46.03Z",
updated: "2022-07-22T08:17:08.8213451Z",
studioAccessToApisAllowed: false,
createVipServersAllowed: false,
universeAvatarType: "MorphToR15",
genre: "All",
isAllGenre: true,
isFavoritedByUser: false,
favoritedCount: 0,
},
],
};
await axios.get("https://games.roblox.com/v1/games?universeIds=3759152694").then(function (response) {
// handle success
console.log(JSON.stringify(response.data[0]));
});
},
};
JSON Table:
{
"data": [
{
"id": 3759152694,
"rootPlaceId": 10269801695,
"name": "unnamed sim",
"description": "soon",
"sourceName": "unnamed sim",
"sourceDescription": "soon",
"creator": { "id": 3593901883, "name": "Jack1286401", "type": "User", "isRNVAccount": false, "hasVerifiedBadge": false },
"price": null,
"allowedGearGenres": ["All"],
"allowedGearCategories": [],
"isGenreEnforced": false,
"copyingAllowed": false,
"playing": 0,
"visits": 0,
"maxPlayers": 6,
"created": "2022-07-18T14:21:46.03Z",
"updated": "2022-07-22T08:17:08.8213451Z",
"studioAccessToApisAllowed": false,
"createVipServersAllowed": false,
"universeAvatarType": "MorphToR15",
"genre": "All",
"isAllGenre": true,
"isFavoritedByUser": false,
"favoritedCount": 0
}
]
}

Aggregate data from nested array

I need help with the aggregate framework.
I have a model (currencies field can contain more than one object):
const schema = new mongoose.Schema({
country: { type: String },
code: { type: String },
region: [{
name: { type: String },
path: { type: Array },
city: [{
name: { type: String },
path: { type: Array },
latitude: { type: String },
longitude: { type: String },
}],
}],
currencies: [{
code: { type: String },
name: { type: String },
symbol: { type: String },
}],
})
And I need to receive all currencies without duplicates.
Received data can view like this:
[
{ code: 'string', name: 'sting', symbol: 'string' },
{ code: 'string', name: 'sting', symbol: 'string' },
...
]
// or like this:
[
currencies: [
{ code: 'string', name: 'sting', symbol: 'string' },
{ code: 'string', name: 'sting', symbol: 'string' },
...
]
]
I try to create a query
Geo.aggregate([
{
$group: {
_id: null,
currencies: { $addToSet: '$currencies' },
},
},
])
but receive this data with duplicates and it has many nested arrays:
[
{
"_id": null,
"currencies": [
[
{
"_id": "5cd9486248989616a411fac5",
"code": "JPY",
"name": "Japanese yen",
"symbol": "¥"
}
],
[
{
"_id": "5cd9491a48989616a411fb47",
"code": "TRY",
"name": "Turkish lira",
"symbol": null
}
],
I try this query:
Geo.aggregate([
{
$addFields: {
code: '$currencies.code',
name: '$currencies.name',
symbol: '$currencies.symbol',
},
},
])
But I receive error "TypeError: item is not iterable".
I need little help )
Db data views like this:
{
"_id": {
"$oid": "5c3334a8871695568817eadf"
},
"country": "Singapore",
"code": "sg",
"region": [
{
"path": [
"Singapore"
],
"_id": {
"$oid": "5c3366c63d92ac6e531e05c0"
},
"city": [],
"name": "Central Singapore Community Development Council"
},
....
],
"__v": 0,
"currencies": [
{
"_id": {
"$oid": "5cd948ec48989616a411fb28"
},
"code": "BND",
"name": "Brunei dollar",
"symbol": "$"
},
{
"_id": {
"$oid": "5cd948ec48989616a411fb27"
},
"code": "SGD",
"name": "Singapore dollar",
"symbol": "$"
}
]
}
In aggregate pipeline first you need to unwind the currencies array and then group them by condition to get desired result.
Geo.aggregate([
{
$unwind: '$currencies'
},
{
$group: {
_id: null,
currencies: { $addToSet: '$currencies' },
},
},
])
For more information you can look into documentation here
db.temp.aggregate([
{$project : {currencies : 1}},
{$unwind: "$currencies"},
{
$addFields: {
currencyHash: {
$concat : ['$currencies.code', "--", "$currencies.name", "--", "$currencies.symbol"]
}
}
},
{
$group: {
_id: "$currencyHash",
currency : {
$first : "$currencies"
}
}
},
{
$project: {
code : "$currency.code",
name : "$currency.name",
symbol : "$currency.symbol"
}
},
{
$project: {
_id : 0,
currency : 0
}
}
]).pretty()

API response in the browser different from the one in the code

Here's my code:
async function BuiltWithCall(website) {
var domainCall = `https://api.builtwith.com/v12/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var domainRes = await fetch(domainCall);
console.log(domainRes);
var keywordCall = `https://api.builtwith.com/kw1/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var keywordRes = await fetch(keywordCall);
console.log(keywordRes);
return await {'domRes': domainRes.json(), 'kwRes': keywordRes.json()};
}
It takes the website provided and runs it through the BuiltWith API. But the problem is the response.
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
PassThrough {
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
_transformState: [Object] },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://api.builtwith.com/v12/api.json?KEY=key&LOOKUP=hotelscombined.com',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
PassThrough {
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: true,
_transformState: [Object] },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://api.builtwith.com/kw1/api.json?KEY=key&LOOKUP=hotelscombined.com',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }
So this right here makes no sense to me. Because this is the exact same URL, run in a browser:
{
"Keywords": [
{
"Domain": "hotelscombined.com",
"Keywords": [
"compare",
"save",
"cheap",
"hotel",
"deal",
"hotelscombined",
"search",
"... More keywords but you get the idea"
]
}
],
"Errors": []
};
Other call response:
{
"Results": [
{
"Result": {
"IsDB": true,
"Spend": 609,
"Paths": [
{
"FirstIndexed": 1294059600000,
"LastIndexed": 1526338800000,
"Domain": "builtwith.com",
"Url": "",
"SubDomain": "",
"Technologies": [
{
"Categories": [
"Edge Delivery Network"
],
"IsPremium": "yes",
"Name": "Amazon CloudFront",
"Description": "Amazon CloudFront delivers your static and streaming content using a global network of edge locations.",
"Link": "http://aws.amazon.com/cloudfront/",
"Tag": "cdns",
"FirstDetected": 1386284400000,
"LastDetected": 1526338800000
},
]
},
]
},
"Meta": {
"Vertical": "Technology And Computing",
"Social": [
"http://twitter.com/builtwith",
"http://facebook.com/builtwith",
"http://linkedin.com/company/builtwith",
"http://google.com/+builtwithdotcom"
],
"CompanyName": "BuiltWith",
"Telephones": [
"+61-300-558745",
"+1-650-618-3949"
],
"Emails": [
"support#builtwith.com"
],
"City": "Sydney",
"State": "NSW",
"Postcode": "2000",
"Country": "AU",
"Names": [
{
"Name": "N/A",
"Type": 0,
"Email": "n/a#builtwith.com"
},
{
"Name": "N/A",
"Type": 0,
"Email": "n/a#builtwith.com"
}
],
"ARank": 22108,
"QRank": 275921
},
"Attributes": {
"MJRank": 8737,
"MJTLDRank": 4620,
"RefSN": 7402,
"RefIP": 10142,
"TTFB": 129,
"Sitemap": 20,
"GTMTags": 0,
"QubitTags": 0,
"TealiumTags": 0,
"AdobeTags": 0,
"CDimensions": 0,
"CGoals": 0,
"CMetrics": 0,
"SourceBytes": 0
},
"FirstIndexed": 1294059600000,
"LastIndexed": 1526338800000,
"Lookup": "builtwith.com"
}
],
"Errors": []
}
So as you can see the responses are completely different, and I have no idea why. The same fetch method works perfectly for PageSpeed API, but here something is going horribly wrong.
PageSpeed call:
async function PageSpeedCall(website) {
var pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${website}&strategy=mobile&key=${keys.pageSpeed}`;
// second call
var results = await fetch(pagespeedCall);
return await results.json();
}
What am I doing wrong?
domainRes is a Response object, not a payload. That is why you see all the stuff in the console output.
To parse the payload as JSON you need to call domainRes.json which also gives you a promise so you have to await for it. Like this.
async function BuiltWithCall(website) {
var domainCall = `https://api.builtwith.com/v12/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var domainRes = await fetch(domainCall);
console.log(domainRes);
var keywordCall = `https://api.builtwith.com/kw1/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var keywordRes = await fetch(keywordCall);
console.log(keywordRes);
return {'domRes': await domainRes.json(), 'kwRes': await keywordRes.json()};
}

Sequelize where condition for multiple tables not working

While building Sequelize query unable to filter data from two tables with two different where conditions. Here I am adding some code...
Models:
//Task model
module.exports = function(sequelize, DataTypes) {
var Task = sequelize.define("Task", {
type: DataTypes.STRING(100),
start : DataTypes.DATE,
finish : DataTypes.DATE,
status : DataTypes.STRING(1),
}, {
classMethods: {
associate: function(models) {
Task.belongsTo(models.User, {
onDelete: "CASCADE",
foreignKey: {
allowNull: true
}
});
Task.hasOne(models.Evaluation);
Task.hasMany(models.Clarification);
}
}
});
return Task;
};
//Evaluation model
module.exports = function(sequelize, DataTypes) {
var Evaluation = sequelize.define("Evaluation", {
agent_id : DataTypes.INTEGER,
agent_name: DataTypes.STRING(100),
status: DataTypes.STRING(100),
}, {
classMethods: {
associate: function(models) {
Evaluation.belongsTo(models.Task, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
Evaluation.hasMany(models.Clarification);
}
}
});
return Evaluation;
};
//Clarification model
module.exports = function(sequelize, DataTypes) {
var Clarification = sequelize.define("Clarification", {
result : DataTypes.TEXT,
status : DataTypes.STRING(100),
comment : DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
Clarification.belongsTo(models.Task, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
Clarification.belongsTo(models.Evaluation, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Clarification;
};
I just added required code not exactly same.
Actually Using:
var filterobj = {
where: {
"UserId": { $ne: "NULL" }
},
include: [{
model: models.Evaluation,
attributes: ["id", "status","agent_name"],
where: {
"status": { $ne: "created" }
},
include: [{
model: models.EvalForm,
attributes: ["id", "name","LobId"],
include: [{
model: models.Lob,
attributes: ["id", "ClientId"]
},
{
model: models.FormDetails,
attributes: ["version", "status"]
}
]
}, {
model: models.Clarification,
attributes: ["id", "status"]
}],
required: true
},{
model: models.User,
attributes: ["name"]
}]
};
models.Task.findAll(filterobj).then(function (tasklist) {
// Doing some stuff for specific format
return res.send(tasklist);
}).catch(function (err) {
return res.send({ "error": { "code": 5000, "message": err.message } });
});
Response:
[
{
"id": 760,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-13T05:41:56.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 760,
"status": "completed",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": [
{
"id": 70,
"status": "raised"
},
{
"id": 71,
"status": "esclate"
}
]
},
"User": {
"name": "pooja.s"
}
},
{
"id": 761,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-12T14:35:30.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 761,
"status": "assigned",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": []
},
"User": {
"name": "pooja.s"
}
}
]
Expected Code(Not working)
var filterobj = {
where: {
"UserId": { $ne: "NULL" }
},
include: [{
model: models.Evaluation,
where: {
"status": { $in: ["assigned","completed"] }
},
attributes: ["id", "status","agent_name"],
include: [{
model: models.EvalForm,
attributes: ["id", "name","LobId"],
include: [{
model: models.Lob,
attributes: ["id", "ClientId"]
},
{
model: models.FormDetails,
attributes: ["version", "status"]
}
]
}, {
model: models.Clarification,
attributes: ["id", "status"]
}],
required: true
},{
model: models.User,
attributes: ["name"]
}, {
model: models.Clarification,
where: {
"status": { $in: ["raised"] }
}
attributes: ["id", "status"]
}]
};
models.Task.findAll(filterobj).then(function (tasklist) {
// Doing some stuff for specific format
return res.send(tasklist);
}).catch(function (err) {
return res.send({ "error": { "code": 5000, "message": err.message } });
});
Expected Response:
[
{
"id": 760,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-13T05:41:56.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 760,
"status": "completed",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": [
{
"id": 70,
"status": "raised"
},
{
"id": 71,
"status": "esclate"
}
]
},
"User": {
"name": "pooja.s"
},
"Clarifications": [
{
"id": 70,
"status": "raised"
}
]
},
{
"id": 761,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-12T14:35:30.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 761,
"status": "assigned",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": []
},
"User": {
"name": "pooja.s"
},
"Clarifications": []
}
]
I can include Clarification table inside Task and Evaluation tables I have that association. The problem with where condition. It is pulling all the records. I am expecting data from Clarification table with status raised. And from Evaluation table status with assigned and completed. Some where I saw code using OR operator.
A.findAll({
where: {
$or: [
{'$B.userId$' : 100},
{'$C.userId$' : 100}
]
},
include: [{
model: B,
required: false
}, {
model: C,
required: false
}]
});
But this is not working for me.

Categories

Resources