How to get 'No User found' message from the below variable in Node.js - json

How to get 'No User found' message from the below variable in Node.js
{"error":["No User found."]}

You can do it like this:
var data = {"error":["No User found."]};
alert(data.error[0]);
The outer object has a property "error". That property, then contains an array from which you want the first element in the array (index 0).
So, you get the error property with data.error. And, then you reach into the array and get the first element with data.error[0].

You need to iterate through this object with a for loop and grab the value based on the name of the property.
for(item in obj){
console.log(obj[item]);
}
You can use an if statement and the match method if you need to make sure that the property name is 'error'.
Here is my fiddle:
http://jsfiddle.net/nhmaggiej/eajqzxfr/

Related

Can not remove an item from an array using Sequelize and MYSQL

I am using MYSQL through Sequelize to build a node.js application with typescript. I created a table and the table has a field which I made a JSON dataType and made it an array by default. I have been able to push items into the array. I would like to remove items from the array, how can I achieve this?
I tried using await category.update({array_Of_food:Sequelize.fn('array_remove',Sequelize.col('array_of_food'),JSON.stringify(category.dataValues.array_Of_food && category.dataValues.array_Of_food[index]))})
I got an error that array_remove does not exist.
I solved my problem this way since I couldn't find an inbuilt way of doing it. I know this is not the best method but at least it works. At the end, I wrote a longer code.
1.Get the string value of the item you want to remove.
const indexString = food.dataValues.food_Name as string;
2.Get the index number of that item inside the array you wish to delete it from:
const index = category.dataValues.array_Of_food?.indexOf(indexString) as number;
3.Create a variable for the the array out of the model colum that you are targeting.
const arrayValue = category.dataValues.array_Of_food
4.Remove that item from the array variable that you crceated:
arrayValue?.splice(index, 1);
5.Use the update method and pass the array variable you deleted the item from as the update: This will replace the initial array values with the new array values. Remember that the new array values contain all the values in the array column excluding the value you deleted.
await category.update({array_Of_food: arrayValue})
This worked!

How to use user defined python function inside a sqlalchemy query filter?

I have a function in python is_valid_id(id) which returns True or False.
I want to use this function in my sqlalchmy query inside filter condition. My query is as below.
result = session.query(tableName).filter(is_valid_id(id) == True).all()
This throwing the following error.
AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'strip'
Can you please tell how to use a user defined function inside sqlalchemy query.
I have tried using func keyword also. That is not working.
Right now you're saying filter by (True == True).all()
This doesn't do anything.
You need to say what you're filtering.
result = session.query(tableName).filter(tableName.columnName == condition).all()
It sounds to me like you should do something like this.
Grab everything, then loop through each object and see if your id returns true.
For example:
table_objects = session.query(tableName).all()
for current_object in table_objects:
current_id = current_object.id
is_current_id_valid = is_valid_id(current_id)
if is_current_id_valid:
print(f'this id is valid: {current_id}')

missing value from json object when writing if condition it's througing an error

my JSON Object is varies based on some conditins. when i am trying to access the value from json object when it is not present it's througing an error message saying that value is not defined. How to avoid such kind of situations??
IntentSlots: '{"cnumberslot":{"name":"cnumberslot","value":"C186206"}}' - condition works perfectly.
IntentSlots: '{"cnumberslot":{"name":"cnumberslot"}}' } - throws an error.
my condition is like below,
if(IntentSlots.cnumberslot.value !== ""){
}
Based on the condition if the property is not available add an empty value to that property because if u leave it empty it will throw that error.
or use different if condition
if(IntentSlots.hasOwnProperty('value') && IntentSlots.cnumberslot.value !== ""){ }

Angular2 IndexOf Finding and Deleting Array Value

Hello I'm attempting delete a certain index in my array while using Angular2 and Typescript. I would like to retrieve the index from the value.
My array is declared normally...
RightList = [
'Fourth property',
'Fifth property',
'Sixth property',
]
I start out with a basic premise to set up my remove function.
removeSel(llist: ListComponent, rlist:ListComponent){
this.selectedllist = llist;
console.log(JSON.stringify(this.selectedllist)); // what is to be searched turned into a string so that it may actually be used
My console.log of my JSON.Stringify tells me that the value that I will attempt to remove is "Sixth property". However when I attempt to look for this value in my array using the following code. It returns -1 which means my value is not found in the array.
var rightDel = this.RightList.indexOf((JSON.stringify(this.selectedllist))); // -1 is not found 1 = found
console.log(rightDel);
On my output to the console it does return the item that is to be searched for but does not find the item in the array
CONSOLE OUTPUT:
"Sixth property" // Item to be searched for
-1 // not found
Is there something wrong in my implementation of my function that searches the array?
Of course indexOf will not find you item in array because
JSON.stringify(this.selectedllist) !== this.selectedllist
This is because JSON stringified string encodes quotes around literal, that original string doesn't have. It is easy to test:
var a = 'test';
console.log( JSON.stringify(a), a, JSON.stringify(a) === a )
Remove JSON.stringify and it should work. In general, to cast something to String type you should use its .toString() method, or simply wrap this something into String(something).

cant set Index ObjectChoiceField (load slow json)

I have a select that I get Json post with http, but I try to sets initially selected index but there is nothing in the list do not select anything. because the json is great.
public AppMainScreen() {
loadLists();
MySelect = new ObjectChoiceField( "Select: ", new Object[0], 3 );
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
vfm.add(MySelect);
add(vfm);
}
This statement appears wrong to me:
new ObjectChoiceField( "Select: ", new Object[0],3);
The second parameter to this constructor is supposed to be an array of objects whose .toString() method will be used to populate the choices. In this case, you have given it a 0 length array, i.e. no Objects. So there is nothing to choose. And then you have asked it to automatically select the 3rd item, and of course there is no 3rd item.
You should correct the code to actually supply an object array.
One option to make it easy is have your JSON load actually create a String array with one entry per selectable item. Then you use the index selected to identify the chosen item.