NodeJS undefined value - string

I can't seem to get this to work with a string
binance.prices(function(ticker) {
console.log("prices()", ticker);
console.log("Price of BNB: ", ticker.BNBBTC);
});
returns the correct value, however,
binance.prices(function(ticker) {
var tickerName = "BNBBTC";
console.log("prices()", ticker);
console.log("Price of BNB: ", ticker.tickerName);
});
returns undefined. I've asked around and haven't been able to get any help on this.

Solved my problem by using ticker[tickerName] instead! Thanks for the help :)

Of course you cannot change it like that.
They are 2 different things
1) var tickerName = "BNBBTC";
this means your variable tickerName value is a string and equal to "BNBBTC"
so ticker.tickerName will not be equal to ticker.BNBBTC (I will explain below)
That is why it return undefined because Node trying to find ticker.tickerName, which means Node trying to find the tickerName attribute from ticker. And your tickerName that you define is a variable, not an attribute of ticker
2) ticker.BNBBTC is a variable that already have a value. Remember, the part of BNBBTC is not a value, nor a string. but it is a attribute of ticker.
Look at the difference, one of them is string, one of them is an attribute to ticker.
if you want to do something like that, maybe you can approach it by using:
binance.prices(function(ticker) {
var tickerName = ticker.BNBBTC;
console.log("prices()", ticker);
console.log("Price of BNB: ", tickerName);
});
Happy Coding!
Addtional:
if you insist want to use ticker.tickerName, you can use this code:
binance.prices(function(ticker) {
var ticker.tickerName = ticker.BNBBTC; #I change this part
console.log("prices()", ticker);
console.log("Price of BNB: ", ticker.tickerName); #I change this part
});

Related

Trying to check to see if something exists and if not, assign something different to it with JavaScript

Wondering if someone can help, I'll try to explain the best I can. I am looking at a json file for various steam games. Basically what I want to do is check to see if a certain "key(?)" is present and if not then return "None".
e.g. Check to see if body[id].data.metacritic and if it does then assign score to body[id].data.metacritic.score. The same with URL. I cannot figure this out!
I have tried the following:
if(bulk.metacritic) var { score, url } = bulk.metacritic[0] || "None";
I just can't figure out how to get this right! Basically, body[id].data.metacritic doesn't exist in all json files, along with other parameters so I just want to display some placeholder text if they don't appear.
You can check if the body[id].data.metacritic is an array with a non-zero length and if so, use a value from it, otherwise use default values:
let score, url;
let data = body[id].data;
if (data && Array.isArray(data.metacritic) && data.metacritic.length > 0) {
score = data.metacritic[0].score;
url = data.metacritic[0].url;
} else {
score = url = "None";
}
console.log(score, url);

MongoClient dot vs standard key value access

I ran into an interesting scenario today working on some code that I've been trying to research with no such luck as of yet. Would like someone to shine some light on the situation if they can please.
Ingredients is a list of JSON objects that is stored in each recipe. dbIngredient is a single ingredient I pull back using a findOne call with MongoClient. The comparison is the if statement that is currently returning false if I have something like tbsp for both values.
recipe.ingredients.forEach(async (ingredient) => {
// This will convert the ingredients as needed and form the grocery list.
let dbIngredient = await db.collection('ingredients').findOne({ 'text_friendly_name': ingredient.text_friendly_name });
if (dbIngredient.most_used_measurement != ingredient.measurement)
{
ingredient.quantity = unitConverter.convertMeasurement(ingredient.quantity, ingredient.measurement, dbIngredient.most_used_measurement);
}
});
Using the dot notation doesn't appear to ever return a true, but if I use bracket notation like so dbIngredient['most_used_measurement'] != ingredient['measurement'] the statement is evaluated as true. Does anyone know why the dot notation fails but the bracket notation doesn't?

firebase equalTo database query command

var ref = db.ref("main")
var usersRef = ref.child("users");
var accountIdVal = 56473;
What datatype is the equalTo query commmand using when I try to search with a variable, like below it does not return the desired result.
usersRef.orderByChild("accountID").equalTo(accountIdVal).once("value",function(snapshot){
//returns all the values in users
});
when the same thing is tried with non referenced value like below it works fine
usersRef.orderByChild("accountID").equalTo(56473).once("value",function(snapshot){
//returns the exact user
});
even when I use accountIdVal.toString() it does not seem to work, am I missing something here?
Hi thanks for the help I figured out the answer it was because I was getting the value of the variable as a Long, it was checking for a Integer and not a string so the datatypes were not matching up.

Checking what a value is in SSJS

I have this block of code in SSJS that I'm doing some field validation stuff:
thisDoc is a NoteXspDocument
fld = the name of a field
var thisValue = thisDoc.getValue(fld);
print("Check Text = " + thisValue);
print("Is is a Date " + (thisValue === Date))
when I run it the log has this:
Check Text = 09/10/15 12:00 PM
Is is a Date false
In this code I do not know what the datatype is of the fld which is a field name. I check the backend document and get the NotesItem.Type() and this field is of type text 1280 in the backend, but the NotesXspDocument has a date. I need to determine what the data type is thisValue sure acts like a NotesDateTime object, but I'm doing something wrong somewhere.
I think the issue might be the difference between a NotesDateTime and a java.util.Date but they drive me up the wall.
Further Edit --
The problem is that I have an Array of field names var Fields:Array that I then loop through and get fld = Fields[n] so when I get the value of the field it could be anything Text, Date, Number so when I do var thisValue = thisDoc.getValue(fld) or thisDoc.getItemValue(fld) I need to figure out what kind of value I have. I guess I could put the getItem..... inside a try until I find one that works but that seems like a less than optimum solution.
Try instanceof Date.class. What you've got is not checking the data type of thisValue against the underlying class, instead it's checking the object itself.
Because the field that I am retrieving can be just about anything I use
var thisValue = thisdoc.getValue(fld);
i had a lot of trouble then determining what kind of data I had. It could be a null Date/Number/String So the first thing I did was find out what the backend data type was:
var thisItem:NotesItem = thisDoc.getDocument().getFirstItem(fld);
var type:Integer = thisItem.getType()
This helps somewhat if the field has been previously set, but if it is a new document or the field has not received a value yet it will be type 1280 or text and probably null.
So my fisrt test is for null or "". then it becomes a bit tougher because I need to test for some values. In all my comboboxs I add the text "--- Select ?????" as the first item in the list so I tried to get a substring of "---" but because of variance in the datatype I needed to put that in a try:
try{
if (thisValue.substring(0,3) == "---"){
print("have null Prefix");
rtn = false;
errMsg.push("The field " + fld + " is a Required Field please enter a value");
break;
}catch(e){ etc
I then wrapped the various other datatype tests in trys and now I have it working.
Might be a better way but this works.
Use .getItemValue() to return a vector array, then test the data type. You can also try .getItemValueString() to return a text string or .getItemValueDate() or .getItemValueDateTime() to return date/time.
Since getItemValue() returns an array, use subscript to get the first element:
var thisValue = thisDoc.getItemValue(fld);
var thisIsDate = (thisValue[0] instanceof Date);
print("Check Text = " + thisValue[0]);
print("Is this a Date ? " + thisIsDate;

How to fill a combobox with many values from different database?

I've tried with #DbColumn but I get Infinity, this is my current code:
var dbName:NotesDatabase = session.getDatabase("the server", "the database.nsf");
var v:NotesView = dbName.getView("the view").getColumnValues(0);
return v;
But this returns about a hundred results and after that I get distorted text, hieroglyphs, values in different lines etc.
A screenshot of the values:
Now what? Thank you very much!
This is something of a guess because I'm not an XPages guy, but it looks to me like you're saying:
var v:NotesView
And then
return v;
So this means you are declaring that v is a NotesView object. You're actually assigning the columnValues, but the data type is wrong and there is apparently no type-checking going on. So then you return v, and it is being treated as a NotesView object instead of as an array of string values.
Perhaps this is what you need:
var v:NotesView = dbName.getView("the view");
return v.getColumnValues(0);

Resources