Saturday, 30 March 2019

reactjs - TypeError: cannot read property setState of undefined



I am receiving the data from server but not able to display it on the browser. I am receiving error as:





caught (in promise) TypeError: Cannot read property 'setState' of undefined




import React from 'react';

import axios from 'axios';
class App extends React.Component {
constructor(props){
super(props);
this.state={

posts:'hello',
dos:[]
}

};


componentDidMount() {
axios.get(`http://192.168.1.9:8082`)
.then(function(data) {

console.log(data);
this.setState({dos:data});
});
}

render() {
return (

Hello World!!!

{this.state.posts}



{this.state.dos}



);
}
}

export default App;

Answer



Its a context issue, your mistake is that you didn't bind all the way down to the anonymous function. What you probably want to do is use arrow functions, try this:




componentDidMount() {
axios.get(`http://192.168.1.9:8082`)
.then(data => this.setState({dos:data}););
}


Arrow functions always keep the context of this.



Alternate solution:




componentDidMount() {
axios.get(`http://192.168.1.9:8082`)
.then(function(data) {
console.log(data);
this.setState({dos:data});
}).bind(this);
}

methods - Does Java support default parameter values?



I came across some Java code that had the following structure:



public MyParameterizedFunction(String param1, int param2)
{

this(param1, param2, false);
}

public MyParameterizedFunction(String param1, int param2, boolean param3)
{
//use all three parameters here
}


I know that in C++ I can assign a parameter a default value. For example:




void MyParameterizedFunction(String param1, int param2, bool param3=false);


Does Java support this kind of syntax? Are there any reasons why this two step syntax is preferable?


Answer



No, the structure you found is how Java handles it (that is, with overloading instead of default parameters).



For constructors, See Effective Java: Programming Language Guide's Item 1 tip (Consider static factory methods instead of constructors) if the overloading is getting complicated. For other methods, renaming some cases or using a parameter object can help. This is when you have enough complexity that differentiating is difficult. A definite case is where you have to differentiate using the order of parameters, not just number and type.


What is the difference between implicit and explicit String declaration in java?

For Example..





String herName = new String("clark");



and



String hisName = "michal";



1) The first piece of code exactly does, it will create new string
object in the heap memory with reference .




2) The second line of code , its an string literal which create
string object in string constant pool, not in actually heap memory.




Then what's the benefit, ?

security - How to detect SQL Injection sitting at a reverse proxy?

I am writing a simple reverse proxy in java. So, I have access to all Http requests and responses exchanged between client and server. Sitting at the proxy I am trying to detect SQL Injection Attack.



I got few links - (like for example)




http://www.symantec.com/connect/articles/detection-sql-injection-and-cross-site-scripting-attacks



where some regex are mentioned, but I suppose it's not that simple.
It is impossible to write regex for all possible/valid SQL statements.
Because so many databases are there in the market and SQL statements must follow some grammer rules.



Let me break down the problem to a simple question -



Given a string, can it be checked that whether it contains a valid SQL statement?




Can anyone tell me the best way to do it? Or, any library which does that for me?

c++ - comparing float variable

When comparing floats, you have to compare them for being "close" instead of "equal." There are multiple ways to define "close" based on what you need. However, a typical approach could be something like:


namespace FloatCmp {
const float Eps = 1e-6f;
bool eq(float a, float b, float eps = Eps) {
return fabs(a - b) < eps;
}
//etc. for neq, lt, gt, ...
}

Then, use FloatCmp::eq() instead of == to compare floats.

What is a "static" function in C?



The question was about plain functions, not static methods, as clarified in comments.



I understand what a static variable is, but what is a static function?



And why is it that if I declare a function, let's say void print_matrix, in let's say a.c (WITHOUT a.h) and include "a.c" - I get "print_matrix@@....) already defined in a.obj", BUT if I declare it as static void print_matrix then it compiles?




UPDATE Just to clear things up - I know that including .c is bad, as many of you pointed out. I just do it to temporarily clear space in main.c until I have a better idea of how to group all those functions into proper .h and .c files. Just a temporary, quick solution.


Answer



static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).



EDIT: For those who thought, that the author of the questions meant a 'class method': As the question is tagged C he means a plain old C function. For (C++/Java/...) class methods, static means that this method can be called on the class itself, no instance of that class necessary.


Friday, 29 March 2019

character - What nationality is Bane? - Movies & TV



It has been clearly established that Ra's Al Ghul & Talia Al Ghul are Moroccan. And it is assumed that The Pit is in Morocco.



Does this mean that we should assume Bane is also Moroccan? Or is it just coincidence that he was in a Moroccan prison and involved with two Moroccans?


Answer




An article written by NBC has the following quote.




DC Comics describes Bane’s father as a British mercenary and his mother a rebel from the Caribbean. His life spent in a jail on the fictional Caribbean Island of Santa Prisca, a seemingly Spanish name.




So basically Bane is half British, half Caribbean which would explain why he is so tanned yet still speaks perfect English.



Link to the article here


php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...