Cloud Function ArrayRemove not deleting array value, but function runs without error - node.js

My cloud function is not deleting value from the array it is suppose to. It gets all the values correctly as I see in my debug console, but the value in the array is still there.
My code statement is as follows:
for (const id of membersUnderStructure){
const ele = context.params.structure;
console.log(`Deleting ${ele} From ${id}`);
await db.collection(`TeamMember`)
.doc(id)
.update({teamStructIds: admin.firestore.FieldValue.arrayRemove(ele)})
I know the problem is with the admin.firestore.... line as if I put a constant value there, it does update fine. My console shows the correct value of 'ele' and 'id' as well, so the values are correct. The function executes 'ok' without any error but does NOT delete the value of 'ele' from the 'teamStructIds' array.
I am not sure what am I doing wrong here?

Solution:
The Array is of 'Number' and it is trying to delete a String of 'ele'.
I just changed to parseInt(context.parama.structure) and it works now.

Related

Node BehaviorSubject Problem only get empty object

I am using Angular version 7 and trying to use a BehaviorSubject
but when I get the value, all times I receive an empty object
value empty
line: 315 | let value: any = await this._service.machine$.pipe(take(1)).toPromise()
Emmit
this._service.emitterMachineSelected('new')
code of service
_machine's default value
_machine is a BehaviorSubject that (to start with) contains an empty object. ({} is an empty object). That's how you read the following line:
_machine = new BehaviorSubject({});
So when you grab that value, an empty object (The default value of the BehaviorSubject) is what you get.
Using .next to emit a value
Some time later, it contains the string "new"
this_machine.next("new");
You're not grabbing this value anywhere, so you shouldn't expect to see it anywhere.

AWS Lambda cannot see keys in object

I am working on a Lambda function that reads data from a file stored on S3 and writes it to an Aurora DB instance.
I can read the file in without a problem but when I try to access the key:value pairs it always comes back undefined.
I can run the code snippet in VSCode and it works.
I have tried every variation JSON.parse, JSON.stringify and any other manipulation I can think of.
Here is a portion of code that inspects the data at various states.
async function rdsWrite(objectRead) {
console.log("rdsWrite object ", objectRead, "typeof ", typeof(objectRead));
let person = new Object(objectRead);
console.log("person object ", person, "typeof ", typeof(person));
let personObj = JSON.parse(person);
console.log("personObj object ", personObj, "typeof ", typeof(personObj));
if(personObj.hasOwnProperty('middlename')){
console.log("has key");
} else {
console.log("does not have key");
}
The first console.log is the data coming directly from the S3 Read function and it returns the data and typeof string
The person console.log also returns the data and a typeof object
The personObj console.log returns the data as an object with a type of object.
I've used hasOwnProperty to see if anything returns the middlename key. I've used several keys.
In this image you can see the formatting of the AWS log. The first one is truncated at the beginning, the second is a single line and the third prints vertically.
I don't really know if the log formats are relevant.
The only thing I really need to know is why can't I get personObj.name or any other key:value pair?
Any help is appreciated. Thanks.
btw, the data is fake from Mockaroo so I'm not sharing any PII.
Well, I guess it was waiting for me to post.
console.log(personObj[0].name)
now returns personObj name Albert Stubbeley

How to get object x or y in nested document

I know this has been asked before but I can't seem to find the answer, I want to get data/object in the nested array.
show image problem
schedule = await Schedule.findById({_id:'5b496ec3444152122c8d839e'})
console.log(schedule.datalayout.section.data.x)
If you want to get the specified field in the image, you need to determine field index in the array as down below:
console.log(schedule.datalayout.section[0].data[0].x)
And also, If you want to get all of the x fields in the data array, you need to write something like this:
for(let singleData of schedule.datalayout.section[0].data){
console.log(singleData.x)
}
// for using 'of' keyword, your function must be a async function.

TypeError: Cannot read property 'substring' of undefined

Written in node.js file
The function is returning the error:
Returns the ACE occupation exhibit identifier
Example of originalID: 46R-002-30BroadJour12_01-12_11
Expected output: 46R-002
/*This function is returning the error:
Cannot read property 'substring' of undefined*/
function splitID(originalID){
var aceid = originalID.substring(0,7);
return aceid;
}
//1. Get the ace exhibit occupation id for each of them and put it in a parallel array.
for (var row in values) {
//split the 5th column using our function
var output = splitID(row[4]);
var result = getOccupation(output);
//now we add the split output to our occupation array.
occupationsToInsert.append(result);
}
If you may refer to the documentation here at MDN, it advises against using for...in for looping over the arrays because it does not give consistent values on return. It rather iterates on the enumerable properties of the concerned object passed to it.
In other words, for (var row in values) would not iterate over each individual rows as expected, but rather the enumerable properties of the values list.
So, for your const array, you can find the enumerable properties by simply doing
Object.getOwnPropertyNames(values)
which would return you the following list :
["0", "length"]
You're essentially trying to access the fourth element of this array which doesn't exist, and thereby it is undefined, causing the error you observe.
The error is telling you the exact problem: originalID is undefined. In your for loop, row[4] is resulting in an undefined value. Verify your values array contains what you are expecting.

Nodejs node-sqlite3 run callback not working

I am trying to perform a delete of a row in sqlite db using nodejs and node-sqlite3 package.
When I run the delete command, and manually check the entries, I can see that the query successfully deleted that row but I cant seem to write the code that confirms this.
This is the query
db.run("DELETE FROM Table1 WHERE id=? AND username=?", [id, user], function(error) {
console.log(error);
});
Regardless of a wrong or right input, it outputs null to the console. If the right details are given, it deletes it and prints null, if wrong id and user are given, it still prints null.
Any ideas on what might be wrong?
Thanks
To my prevoius question, the problem was that I've used fat arrow for callback declaration. From javascript documentation I've discovered that in arrow function (fat arrow ), this has lexical scope and so this result undefined and not valued as in library documentation said. Using otherwise anonimous function, this is bounded in dynamic scope and so this.changes is valued.
Now, with code as below, is ok:
var sql = 'update recipes set stars = stars + 1 where id = ?';
db.run(sql,
[
1 // id = 1 execute update - if id = 11111 do nothing
], function(err) {
if(err)
throw err;
console.log("VALUE CHANGES: " + this.changes + " - " + util.inspect(this, { showHidden: false, depth: null }));
if(this.changes == 1)
console.log("WORK DONE");
else
console.log("NOTHING DONE");
});
Here more explanations: https://github.com/mapbox/node-sqlite3/issues/606
There is nothing wrong in the node and node-sqlite3 behaviour here.
Here are two parts to explain first regarding node and other regarding Sqlite.
Node
Your callback is getting called after execution of the statement. So nothing wrong here, since your callback is getting called (as proved by 'null' as output).
Sqlite
Delete query in Sqlite deletes if condition given in where clause evaluates to true, otherwise nothing is deleted.
Referring from node-sqlite3 documentation's Database#run api:
callback (optional): If given, it will be called when an error occurs
during any step of the statement preparation or execution, and after
the query was run. If an error occurred, the first (and only)
parameter will be an error object containing the error message. If
execution was successful, the first parameter is null.
So, in your case query execution succeeds without any error, resulting in error argument to callback function null as you see in output.
Further, if you want to check if any row was actually removed, you can use changes property as mentioned in the documentation:
If execution was successful, it contains two properties named "lastID"
and "changes" which contain the value of the last inserted row ID and
the number of rows affected by this query respectively. Note that
"lastID" only contains valid information when the query was a
successfully completed INSERT statement and "changes" only contains
valid information when the query was a successfully completed UPDATE
or DELETE statement. In all other cases, the content of these
properties is inaccurate and should not be used. The .run() function
is the only query method that sets these two values; all other query
methods such as .all() or .get() don't retrieve these values.
Hope it helps...
I had similar problem, callbacks just would not fire. Period. The problem was that elsewhere I was calling process.exit(1), so my code was exiting before the the callbacks had a chance to return.
Search for process.exit, that may (or may not) save you hours of debugging and blaming sqlite :)
Off the topic: What bugs my mind is why they all-cap ID in lastID. It's not like it's an abbreviation like SQL or USA. It stands for Identification, which is one word.

Resources