Key not being recognized in JSON - node.js

I have following JSON that i am trying to parse.
{"id":1,"colour":"blue","count(colour)":1}
It is a result of what i have returned from my sqlite3 select staement. I am doing a count(colours), which is being returned as the key in the JSON.
Then when i attempt to reference the value using .count(colour), my node app fails giving me an error that colour is not defined. Note, that referencing .id works just fine.
Has anybody ran into this issue before or can provide any help?

If your JSON is in some variable, myjson, and you access it directly with myjson.count(colours), you get the error because it's trying to execute a function in your object.
It works as you expect if you access via string like this: myjson["count(colour)"].

Related

accessing nested objects in mongo using node.js

I'm unable to access a nested object in a mongo doc. None of the online solutions or even mongo documentation has been able to resolve my problem.
{
characterName:character.characterName,
0:{slotName:"", slotCount:0},
1:{slotName:"", slotCount:0},
2:{slotName:"", slotCount:0},
3:{slotName:"", slotCount:0}
}
I want to access any of the numbered keys via an api and change the value of slotCount.
I've tried everything I could find online including the below.
db.Inventories.updateOne(
{characterName:user.selectedCharacter.CharacterName},
{$set:{[req.query.slotNumber].0.slotCount:req.query.itemCount}}
);
I'm getting syntax errors from VSCode as it's not expecting a period . after the first key on the last line of code.
SOLUTION: create a var that stores a string
key =`${req.query.slotNumber}.slotCount`
and then use square brackets when calling the key
db.Inventories.updateOne(
{characterName:user.selectedCharacter.CharacterName},
{$inc:{[key]:-1}}

Problem accesing a dictionary value on dialogflow fullfilment

I am using fullfilment section on dialogflow on a fairly basic program I have started to show myself I can do a bigger project on dialogflow.
I have an object setup that is a dictionary.
I can make the keys a constant through
const KEYS=Object.keys(overflow);
I am going through the values using
if(KEYS.length>0){
var dictionary = overflow[keys[i]]
if I stringify dictionary using
JSON.stringify(item);
I get:
{"stack":"overflow","stack2":"overflowtoo", "stack3":3}
This leads me to believe I am actually facing a dictionary, hence the name of the variable.
I am accesing a string variable such as stack and unlike stack3
Everything I have read online tells me
dictionary.stack
Should work since
JSON.stringify(item);
Shows me:
{"stack":"overflow","stack2":"overflowtoo","stack3":3}
Whenever I:
Try to add the variable to the response string.
Append it to a string using output+=${item.tipo};
I get an error that the function crashed. I can replace it with the stringify line and it works and it gives me the JSON provided so the issue isnt there
Dictionary values are created as such before being accessed on another function:
dictionary[request.body.responseId]={
"stack":"overflow",
"stack2":"overflowtoo",
"stack3":3 };
Based on the suggestion here I saw that the properties where being accessed properly but their type was undefined. Going over things repeatedly I saw that the properties where defined as list rather than single values.
Dot notation works when they stop being lists.
Thanks for guiding me towards the problem.

Cloud Firestore node.js invalid use of type "object" as a Firestore argument

My question is about why I can't seem to pass data into a Firestore database. I'm running code on node.js trying to parse data coming from my Firebase Firestore database and then return information back to the database. Retrieval of the information works fine, but an error message that I cannot escape for the life of me keeps appearing when trying to pass data back to the database.
if(message.type = "0") {
console.log(0)
db.collection("outbox").doc("messageID").set({message: "test message"})
}
On trying to process the last line of code, the following error message appears:
Error: Argument "data" is not a valid Firestore document. Invalid use of type "object" as a Firestore argument.
My database is structured as outbox/messageID/message. Message holds a string value and I'm using the admin SDK so write issues aren't a problem. I'm doing something wrong with passing the object along to Firebase but I just can't figure it out. This is my first time working with Cloud Firestore and node.js but I've done small-scale projects in HTML/JS before.
It looks like an internal error with the way firestore checks if a value is an array.
I have the same issue trying a simple
db.collection('Test').add({ foo: 'bar' })
Which leads to
Error: Value for argument "data" is not a valid Firestore document. Invalid use of type "object" as a Firestore argument
After digging into firebase source code i found they check if an object is an object, based on this test :
Object.getPrototypeOf(input) === Object.prototype
Which is false in some environments for some unknwon reasons (In my case: Node v10.15.3 CLI)
As a workaround if you temporary needs to have this works, you may edit your node module source code in this file : node_modules/#google-cloud/firestore/build/src/serializer.js
Change the isPlainObject function to :
function isPlainObject(input) {
return util_1.isObject(input);
}

KnexJS Raw Method doesn't work in one case

Working with KnexJS on a project and have been using the .raw() method throughout the project with out a single issue.
I now have one case where the .raw is just not being built into the SQL, and so I end up with the syntax error at end of input error. If I dump the SQL string, I can see why as it's failing
'update "mytable" set "a" = ? returning '
I can see why it's having an issue, the problem I'm having is that the returning is a Raw value as such, and so I just can't figure out why it's not being compiled with the SQL.
knexQuery.update(data).into('mytable').returning( knexQuery.raw('mytable::json') );`
If I use a string, in place of the raw in the returning() method, it will compile the string into it.
If I console out the knexQuery.raw('mytable::json') part, it shows as a Raw object, with the right object data...
Raw {
client:
Client_PG {
... },
sql: 'mytable::json',
bindings: undefined,
_wrappedBefore: undefined,
_wrappedAfter: undefined,
_debug: undefined } }
I know that the SQL works, as I've filled in the missing part and it works, but the SQL is not the issue, I just can't figure out why the raw() method is not being compiled with the SQL string.
I also have another piece of code (and INSERT one) somewhere else in my code that is using the same returning( knexQuery.raw(....) ) and that works perfectly fine.
I'm starting to think this is a bug in the code, but after spending an hour going through the KnexJS library code, I can't see any reason why it would not work.
So why does this code not build the raw into the query, while my other one has no problem and works?
I'm getting to the point where I just want to use something else to get around the problem, but it's just not possible without using this method.
This is bug in knex https://runkit.com/mikaelle/592412c3a631940012a51928 please open issue in knex github. Looks like handling raw input just haven't been implemented to returning builder method.
Anyways looks like this works as a workaround:
knex('TestTable')
.insert({ foo: 'bar' })
.returning([knex.raw('mytable::json')]) // raw wrapped in array
.toSQL();

group by in Flask-MongoAlchemy

i stared new project with flask web-framework with mongoDB. i also used database and access data using Flask-MongoAlchemy. i tried work on different query like .all(), .filter_by(), .get() its work nice. but problem is that how to use aggregate funcation in Flask-MongoAlchemy ? for example i want to use group_by.
i tried following but its still not working.
db.User.qroup_by(name)
its gives following error
'BaseQuery' object has no attribute 'group_by'

Resources