Access $firebaseObject value with variable instead of string - angularjs

I need to assign a variable to a string and pass that variable to a Firebase query in a $firebaseObject. But, when I try this, the firebaseObject is null. I tried with directly putting the string in and it works, but I don't want that.
Code:
//var itemkey='-JyO7Zsgsucf5ESttJwt';
var itemkey=window.localStorage['fbid'];-------> string: '-JyO7Zsgsucf5ESttJwt'
console.log(typeof itemkey); ------------------> string
$scope.galphoto = $firebaseObject(ref.child(itemkey));
console.log($scope.galphoto) ------------------> null
When I call
$scope.galphoto = $firebaseObject(ref.child(itemkey));
With itemkey='-JyO7Zsgsucf5ESttJwt',
then console.log($scope.galphoto) is properly shown. However, when I use what I really want to use,
window.localStorage['fbid']
then console.log($scope.galphoto) is null.
Why?

I think you may be trying to console.log the result before it's available. $firebaseObject returns the data with some delay. You may try to do something like
$scope.galphoto = $firebaseObject(ref.child(itemkey));
$scope.galphoto.$loaded().then(function () {
console.log($scope.galphoto);
}
You should see the expected result, since you're waiting for the promise to respond. It could help you better understand how to get the desired result.

Related

Why does the .lower() in my if var in array: statement not work?

if movie.lower() in movieList:
if (youSure.lower()=="yes"):
print("Removing '"+movie+"'")
movieList.remove(movie)
My problem is that when running this, the program completely skips over the initial if statement
if movie.lower() in movieList:
I have tried using .lower() on both movie and movieList individually as well as both at the same time, however, this hasn't had any effect.
The problem is .lower() function does not do operation inplace, maybe because strings are immutable in python(not sure if its the right reason).
movie_lowercase = movie.lower()
if movie_lowercase in movieList:
if (youSure.lower()=="yes"):
print("Removing '"+movie+"'")
movieList.remove(movie)

Parsing and converting JSON

I have an issue with TJSONObject.ParseJSONValue or TJSONObject.toString
the following code does not display the JSON from Memo1 in Memo2, only an empty "{}":
A:=tJSONObject.Create;
A.ParseJSONValue(Memo1.Lines.Text);
Memo2.Lines.Text:=A.ToString;
What is wrong with it?
ParseJSONValue() is a class function of TJSONObject. It does not modify the TJSONObject that it is called on, like you are expecting. It returns a new TJSONValue, which will point to a TJSONObject if the JSON data represents a JSON object. You are ignoring that return value (and thus leaking it, as you need to call Free on it with you are done using it).
You need to change your code to something more like this:
var A: TJSONValue;
A := TJSONObject.ParseJSONValue(Memo1.Text);
if A <> nil then
try
Memo2.Text := A.ToString;
finally
A.Free;
end;
ParseJSONValue is a FUNCTION so it needs to be
A:=A.ParseJSONValue...
how stupid....

Why is my object not changing when I reassign properties in a forEach loop?

I am fetching an array of objects from an RX/JS call from an http backend. It returns an object which I am then trying to work with. I am making changes to this object using a for loop (in this example I am trying the .forEach because I have tried a number of different things and none of them seem to work.
When I run the code, I get a very weird problem. If I return the values of the properties, I get the new values (i.e. correctionQueued returns as true, etc.) but in the very next line, when I return the object, those same values are the same as the original (correctionQueued === false, etc.) HOWEVER, correctionStatus (which does not exist on the original object from http) sets just fine.
I don't understand how
array[index].correctionQueued can return true, but
array[index] returns an object with correctionQueued as false.
After the loop, the original array (checklistCopy) is identical to the object before the forEach loop, except the new property (correctionStatus) is now set, but all properties that I changed that were part of the original object remain as they were.
I have tried using a for of, for in, and .forEach. I have used the index to alter the original array, always the same result. Preexisting properties do not change, new properties are added. I have even tried working on a copy of the object in case there is something special about the object returned from rxjs, but to no avail.
checklistCopy.forEach((checklistItem, index, array) => {
if (checklistItem.crCode.isirName === correctionSetItem) {
array[index].correctionQueued = true;
array[index].correctionValue = mostRecentCorrection.correctionValue;
array[index].correctionStatus = mostRecentCorrection.status;
console.log(array[index].correctionQueued, array[index].correctionValue, array[index].correctionStatus);
console.log(array[index]);
}
}
);
I don't get an error, but I get..
Original object is:
correctionQueued: false;
correctionValue: JAAMES;
--
console.log(array[index].correctionQueued, array[index].correctionValue, array[index].correctionStatus);
true JAMES SENT
but when I print the whole object:
console.log(array[index]);
correctionQueued: false;
correctionValue: JAAMES;
correctionStatus: "SENT'; <-- This is set correctly but does not exist on original object.
console.log(array[index]) (at least in Chrome) just adds the object reference to the console. The values do not resolve until you expand it, so your console log statement is not actually capturing the values at that moment in time.
Change your console statement to: console.log(JSON.stringify(array[index])) and you should discover that the values are correct at the time the log statement runs.
The behavior you are seeing suggests that something is coming along later and changing the object properties back to the original value. Unless you show a more complete example, we can't help you find the culprit. But hopefully this answers the question about why your logs show what they show.
Your output doesn't make sense to me either but cleaning up your code may help you. Try this:
checklistCopy.forEach(checklistItem => {
checklistItem.correctionQueued = checklistItem.crCode.isirName === correctionSetItem;
if (checklistItem.correctionQueued) {
checklistItem.correctionValue = mostRecentCorrection.correctionValue;
checklistItem.correctionStatus = mostRecentCorrection.status;
console.log('checklistItem', checklistItem)
}
}
);

Await Buffer.from(String) turns out to numbers (Javascript)

i am trying desperately to solve the following problem. I researched a lot already but nothing really solved the problem (the used language is Javascript with Node.js and react-bootstrap library).
I want to write the following Array to Buffer and save it to IPFS in a way, that i can read it out later.
Nevertheless, the IPFS.Add() Method demand a buffer object, so i struggle to create the buffer object.
Here the array:
const line_supplier = new Array({
Lieferant: "Yello" ,
Postleitzahl: "13752" ,
Arbeitspreis: "5" ,
Grundpreis: "10" ,
Email: "email"
});
Sounds pretty easy, i know. If i do it like this, the console shows me a an empty buffer.
const line_buffer = await Buffer.from(line_supplier);
console.log(line_buffer);
I also tried ..line_supplier[0], or the recommendations with ..'UTF-8' as offset. I also declared the array directly in Buffer.from(declaration).
Either way, i get an error or just a couple of numbers out. Maybe this is already the mistake. I expect the buffer to be readable like a string?
So i converted the line supplier to a string with
const line_string = JSON.stringify(line_supplier);
But even with this string injected in Buffer.from(), i only get a Uint8Array(84) Object out with a lot of numbers.
I dont know what i am missing. It must be something very small. I read all the description of the methods already, but cant find it.
Thanks in advance!!!
The solution is, the following:
const line_string = JSON.stringify(line_supplier);
console.log(line_string);
const line_buffer = await Buffer.from(line_string);
console.log(line_buffer);
await ipfs.add(line_buffer, (err, ipfsHash) => {
console.log(err,ipfsHash);
Hence you get an array saved the way you want!

bug with IEntity.toString()?

There seem to be a bug with calling .toString() on an entity created by createQueryEntity. If you use it in a query. It will have some garbage string "tring" in it.
IEntity queryEntity = (IEntity) GenerateQuery.createQueryEntity(Vendor.class);
queryEntity.toString();
Calendar start = Calendar.getInstance();
start.add(Calendar.YEAR, -1);
Vendor vendor = (Vendor) queryEntity ;
return select($(vendor))
.where($(vendor.getMetaData().getLastUpdatedTime()).gte(start))
.where($(vendor.isActive()).eq(false))
.skip(1).take(1000)
.generate();
This would result to something like "select tring.tring.* from ..." Has anyone else seen this?
Finally figured it out. Apparently you can't call any methods on the queryEntity returned by createQueryEntity BEFORE calling select(). That means toString() or ((Vendor) queryEntity).isActive() shouldn't be called before select ($(vendor)).
The solution is to store the results of select($(vendor)) first before calling these methods.
Be careful when debugging because adding a watch expression will call toString() too.

Resources