NodeJS module - populated arrays turning into empty objects. Why? - javascript

I have a module which exports a config object:
module.exports = {
music : {
catalog : {
mysql : {
requiredFields : {
foo : [1,2,3],
trackQuery : [
{
table : 'tracks',
alias : 't',
foo : [1,2,3],
fields : [
'id',
'name',
'duration'
]
},
{
table : 'artists',
alias : 'a',
fields : [
'id',
'name'
]
}
]
}
}
}
}
}
The fields arrays are turning into empty objects at runtime.
I'm confirming like this:
var conf = require('musicConfig');
console.log ("requiredFields = %j", conf.music.catalog.mysql.requiredFields);
...which outputs this:
requiredFields = ["foo":[1,2,3],"trackQuery":[{"table":"tracks","alias":"t","foo":{},"fields":{}},{"table":"artists","alias":"a","fields":{}}]]
As you can see:
conf.music.catalog.mysql.requiredFields.foo // [1,2,3]
conf.music.catalog.mysql.requiredFields.trackQuery[0].foo // {} <-- WTF
conf.music.catalog.mysql.requiredFields.trackQuery[0].table // "tracks"
Any idea what's going on? Yes, I can move the fields array to a higher level namespace and it will work - one step up actually. If I put it directly under requiredFields it will stay populated, but that's not an ideal solution.
I've confirmed here that it's not an ECMAScript bug and it's not my object structure, because it works as expected.
I'm running Node 0.10.3 on Ubuntu with these dependencies:
"dependencies" : {
"express" : "3.1.0",
"redis" : "0.8.2",
"jade" : "0.28.2",
"mysql" : "2.0.0-alpha7",
"mongodb" : "*",
"config" : "0.4.22"
}
I thought it might be that config module, but even when I bypass it the issue remains.
UPDATEHere is the output from: console.log(util.inspect(config.music.catalog.mysql.requiredFields.trackQuery[0], { showHidden: true, depth: null }));
-- though it doesn't really help much. I'm looking into more helpful flags, open to suggestions.
{ table: [Getter/Setter],
alias: [Getter/Setter],
foo: [Getter/Setter],
fields: [Getter/Setter],
[__watchers]:
{ table:
[ { [Function]
[length]: 0,
[name]: '',
[arguments]: null,
[caller]: null,
[prototype]: { [constructor]: [Circular] } },
[length]: 1 ],
alias:
[ { [Function]
[length]: 0,
[name]: '',
[arguments]: null,
[caller]: null,
[prototype]: { [constructor]: [Circular] } },
[length]: 1 ],
foo:
[ { [Function]
[length]: 0,
[name]: '',
[arguments]: null,
[caller]: null,
[prototype]: { [constructor]: [Circular] } },
[length]: 1 ],
fields:
[ { [Function]
[length]: 0,
[name]: '',
[arguments]: null,
[caller]: null,
[prototype]: { [constructor]: [Circular] } },
[length]: 1 ] },
[__propertyValues]:
{ table: [ 'tracks', [length]: 1 ],
alias: [ 't', [length]: 1 ],
foo: [ { [__watchers]: {}, [__propertyValues]: {} }, [length]: 1 ],
fields: [ { [__watchers]: {}, [__propertyValues]: {} }, [length]: 1 ] } }

I can reproduce the problems when the file is read using the config module, so I don't think you're bypassing it correctly.
Also, when I increase DEFAULT_CLONE_DEPTH in config/lib/config.js (it doesn't seem to be configurable from the outside, but I'm not very familiar with it), it works:
> console.log(conf.music.catalog.mysql.requiredFields.trackQuery[0].foo)
[ [Getter/Setter], [Getter/Setter], [Getter/Setter] ]

Related

Move elements within MongoDB document

Background:
A customer is an object that has a name field.
A line is an object that has the following fields:
inLine - an array of customers
currentCustomer - a customer
processed - an array of customers
The collection 'line' contains documents that are line objects.
Problem:
I'm trying to implement a procedure which would do the following:
Push currentCustomer to processed
Set currentCustomer to the 1st element in inLine
Pop the 1st element of inLine
Since the new value of a field depends on the previous value of another, atomicity is important here.
What I tried so far:
Naive approach
db.collection('line').findOneAndUpdate({
_id: new ObjectId(lineId),
}, {
$set: {
currentCustomer: '$inLine.0',
},
$pop: {
inLine: -1,
},
$push: {
processed: '$currentCustomer',
},
});
However, currentCustomer is set to a string which is literally "$inLine.0" and processed has a string which is literally "$currentCustomer".
Aggregation approach
db.collection('line').findOneAndUpdate({
_id: new ObjectId(lineId),
}, [{
$set: {
currentCustomer: '$inLine.0',
},
$pop: {
inLine: -1,
},
$push: {
processed: '$currentCustomer',
},
}]);
However, I got the following error:
MongoError: A pipeline stage specification object must contain exactly one field.
Multi-stage aggregation approach
db.collection('line').findOneAndUpdate({
_id: new ObjectId(lineId),
}, [{
$set: {
currentCustomer: '$inLine.0',
},
}, {
$pop: {
inLine: -1,
},
}, {
$push: {
processed: '$currentCustomer',
},
}]);
However, $pop and $push are Unrecognized pipeline stage names.
I tried making it using only $set stages, but it ended up very ugly and I still couldn't get it to work.
Based on turivishal's answer, it was solved like so:
db.collection('line').findOneAndUpdate({
_id: new ObjectId(lineId),
}, [{
$set: {
// currentCustomer = inLine.length === 0 ? null : inLine[0]
currentCustomer: {
$cond: [
{ $eq: [{ $size: '$inLine' }, 0] },
null,
{ $first: '$inLine' },
],
},
// inLine = inLine.slice(1)
inLine: {
$cond: [
{ $eq: [{ $size: '$inLine' }, 0] },
[],
{ $slice: ['$inLine', 1, { $size: '$inLine' }] },
],
},
// if currentCustomer !== null then processed.push(currentCustomer)
processed: {
$cond: [
{
$eq: ['$currentCustomer', null],
},
'$processed',
{
$concatArrays: [
'$processed', ['$currentCustomer'],
],
}
],
},
},
}]);
I don't think its possible with simple update using $push or $pop.
As per your experiment, the aggregation can not support direct $push, $pop stage in root level, so I have corrected your query,
currentCustomer check condition if size of inLine is 0 then return null otherwise get first element from inLine array using $arrayElemAt,
inLine check condition if size of inLine is 0 then return [] otherwise remove first element from inLine array using $slice and $size
processed concat both arrays using $concatArrays, $ifNull to check if field is null then return blank array, check condition if currentCustomer null then return [] otherwise return currentCustomer
db.collection('line').findOneAndUpdate(
{ _id: new ObjectId(lineId), },
[{
$set: {
currentCustomer: {
$cond: [
{ $eq: [{ $size: "$inLine" }, 0] },
null,
{ $arrayElemAt: ["$inLine", 0] }
]
},
inLine: {
$cond: [
{ $eq: [{ $size: "$inLine" }, 0] },
[],
{ $slice: ["$inLine", 1, { $size: "$inLine" }] }
]
},
processed: {
$concatArrays: [
{ $ifNull: ["$processed", []] },
{
$cond: [
{ $eq: ["$currentCustomer", null] },
[],
["$currentCustomer"]
]
}
]
}
}
}]
);
Playground

js add object to object with $or and get [object]

I try to generate an object with {$or: [{object1}, {object2},...]} to fire a query to mongo, but I always get [object] after assigning to $or. Can somebody help me and tell me what I´m doing wrong. I tried it with creating an own array for the $or[], with a for-loop and so on - always the same. Some ideas?
if(req.body.sicDiv) {
co_sicDiv = {
$in: sicDiv.split(',')
};
query = Object.assign({co_sicDiv: co_sicDiv}, query);
//works fine, I get: { co_sicDiv: { '$in': [ 'C' ] }
}
if(req.body.sicMajor) {
if (sicDiv){
co_sicMaj = {
$in: sicMaj.split(',')
};
console.log('co_sicMaj: ', co_sicMaj);
sicMajO =Object.assign(
{'co_sicDiv': co_sicDiv},
{'co_sicMaj' : co_sicMaj}
);
console.log('SicMajO: ', sicMajO);
//Works fine, I get: SicMajO: { co_sicDiv: { '$in': [ 'C' ] }, co_sicMaj: { '$in': [ '15', '16' ] } }
sicMajOR =Object.assign({$or: [
{'co_sicDiv': co_sicDiv},
{'co_sicMaj' : co_sicMaj}
]}
);
console.log('SicMajOR: ', sicMajOR);
//Doesn´t work, I get: SicMajOR: { '$or': [ { co_sicDiv: [Object] }, { co_sicMaj: [Object] } ] }

Node.js - Access key from object

I try to access a key from an object I get back from an API but when I do so, I get the key of the object in a strange format.
This is what I get when I console.log the object (booking):
{ createdDate: 2018-03-26T11:36:09.694Z,
date: 2018-03-26T13:45:00.000Z,
...,
vouchers:
List [
{ value: 100,
code: 'vouchercode',
...
}
]
}
When I then try to console.log the "vouchers" key:
booking.vouchers
I get this:
{ [Function: f]
_receiver: {
createdDate: 2018-03-26T11:36:09.694Z,
date: 2018-03-26T13:45:00.000Z,
...,
vouchers: List [ [Object] ] },
_scope:
{ where: { bookingId: 5ab8db29b24991b50704445a },
collect: 'voucher',
include: 'voucher' },
_targetClass: 'Voucher',
find: [Function],
getAsync: [Function],
...,
}
Why do I get it in this format and how do I convert it to a normal object like this?:
vouchers: [
{ value: 100,
code: 'vouchercode',
...
}
]
I hope the problem is understandable and some of you can help :)
Edit:
This is my code:
Booking.findById id, {include:[ 'vouchers']}, (err, booking)->
console.log booking
vouchers = booking.vouchers
console.log vouchers
*Note: I know this is coffeescript but I don't think thats the problem

Storing data from JSON object received from Google People Api

So I have a server that receives data from Google People api regarding contacts and my received object has the following structure:
{ connections:
[ { resourceName: 'people/c3904925882068251400',
etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDFZUOUE0NkRBZW0wPQ==',
names:
[ { metadata: { primary: true, source: [Object] },
displayName: 'Mihai Vrincut',
familyName: 'Vrincut',
givenName: 'Mihai',
displayNameLastFirst: 'Vrincut, Mihai' },
{ metadata: { source: [Object] },
displayName: 'Mihai Vrincut',
familyName: 'Vrincut',
givenName: 'Mihai',
displayNameLastFirst: 'Vrincut, Mihai' } ],
emailAddresses:
[ { metadata: { primary: true, source: [Object] },
value: 'mihai.vrincut#gmail.com' } ] },
{ resourceName: 'people/c3275206487406036814',
etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDHBFVzBUMm8wWU5nPQ==',
names:
[ { metadata: { primary: true, source: [Object] },
displayName: 'aaaaaaaaa',
givenName: 'aaaaaaaaa',
displayNameLastFirst: 'aaaaaaaaa' } ] },
{ resourceName: 'people/c5777943907795350059',
etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDGxOeGYwblg3bFUwPQ==',
names:
[ { metadata: { primary: true, source: [Object] },
displayName: 'costin',
givenName: 'costin',
phoneticFamilyName: 'cancius',
phoneticGivenName: 'costin',
displayNameLastFirst: 'costin' } ],
emailAddresses: [ { metadata: { primary: true, source: [Object] }, value: 'hj' } ],
phoneNumbers:
[ { metadata: { primary: true, source: [Object] },
value: '07543532512',
canonicalForm: '+40754353251' } ] } ], totalPeople: 3}totalItems: 3 }
In order to get this object I used the util.inspect() method. However, when I try to access the names for example, I get undefined:
var response=util.inspect(responses,{depth:5});
Console.log(response.connections[0].names);
What is wrong?
So, given the situation, and the information you've given over the comment sections.
I assume that responses is already an object, but util.inspect, makes it a string with a JSON kind of syntax but without the quotes (") before and after the names of the keys. That's why you get
{ connections: ^ SyntaxError: Unexpected token c in JSON at position 2
So, try going over the responses object.
console.log(responses)
And get the name of the keys. With them
console.log(responses.sth.sthElse.anotherSth.anotherSthElse.lastSth.connections)
And see if you get the expected result :)
You should convert the response to JSON Object.
try this:
console.log(JSON.parse(response).connections[0].names);
(I am assuming you are working in Javascript)
What I would do is validate if the answer is a String, you have a
console.log (typeof response)
if it is a string, convert it to JSON:
let responseObject = JSON.parse (response);
Finally, try if you can access the object:
console.log (responseObject.connections [0] .names);
You tell me your answer :)

XML Attribute not working in Node.js xml2js

I'm trying to parse XML with Node.js and xml2js. In the documentation is says that $ is the character to access attributes. It doesn't seem to be working in my case.
The object result.ApiResponse.CommandResponse works fine. But anything I put afterwards is undefined.
Here's my code, it says $ is undefined :
var xml2js = require('xml2js');
var util = require('util');
var parser = new xml2js.Parser();
var xml = '<ApiResponse Status="OK"><Errors/><Warnings/><RequestedCommand>namecheap.domains.check</RequestedCommand><CommandResponse Type="namecheap.domains.check"><DomainCheckResult Domain="us.xyz" Available="true" ErrorNo="0" Description="" IsPremiumName="true" PremiumRegistrationPrice="13000.0000" PremiumRenewalPrice="13000.0000" PremiumRestorePrice="65.0000" PremiumTransferPrice="13000.0000" IcannFee="0.0000" EapFee="0.0000"/></CommandResponse><Server>PHX01APIEXT01</Server><GMTTimeDifference>--5:00</GMTTimeDifference><ExecutionTime>4.516</ExecutionTime></ApiResponse>';
parser.parseString(xml, function (err, result) {
console.log(util.inspect(result.ApiResponse.CommandResponse.DomainCheckResult.$.Available, false, null))
});
Here's the console.log(result):
{ ApiResponse:
{ '$': { Status: 'OK' },
Errors: [ '' ],
Warnings: [ '' ],
RequestedCommand: [ 'namecheap.domains.check' ],
CommandResponse:
[ { '$': { Type: 'namecheap.domains.check' },
DomainCheckResult:
[ { '$':
{ Domain: 'us.xyz',
Available: 'true',
ErrorNo: '0',
Description: '',
IsPremiumName: 'true',
PremiumRegistrationPrice: '13000.0000',
PremiumRenewalPrice: '13000.0000',
PremiumRestorePrice: '65.0000',
PremiumTransferPrice: '13000.0000',
IcannFee: '0.0000',
EapFee: '0.0000' } } ] } ],
Server: [ 'PHX01APIEXT01' ],
GMTTimeDifference: [ '--5:00' ],
ExecutionTime: [ '4.516' ] } }
It looks like CommandResponse and DomainCheckResult are actually arrays, so you need to access their first elements using [0] before digging deeper into your data.
console.log(util.inspect(
result.ApiResponse.CommandResponse[0].DomainCheckResult[0].$.Available,
false, null
))

Categories

Resources