Using NodeJS and MongoDB.
I have a mongo query and I am trying to assigned some of the properties of my query to an existing Object, which was the result of a previous query.
I am getting "TypeError: Cannot set property 'property1' of undefined"
enter code here
for(var i=0; i<data.data.length; i++){
if(data.data[i].a){
var c = data.data[i].a.toString();
SOMETHING.METHOD(c, function(user){
console.log(JSON.stringify(user, null, 2));
data.data[i].property1 = user.property1;
data.data[i].property2 = user.property2;
data.data[i].property3 = user.property3;
});
}else{
data.data[i].property1 = "not available";
data.data[i].property2 = "not available";
data.data[i].property3 = "not available";
}
strange thing though, is that when I assign "not available" to property1, there is no error. I have tried changing user.property1 toString() and that didn't solve it. Not sure why one assignment works and the other does not.
Solved it. A classic newb nodejs mistake. Since its asynchronous, the loop was finished before the DB query returned the results, so by the time that part of the function ran, there was no more loop to append the values to.
Related
I have an input where if a user searches for id and if it matches the id, it will display the status. But it gives me an error if a user doesn't give the right ID. What should I do so that even if the user searches for invalid it will just display "Invalid id".
My array
What my code looks like
According to your code, I have seen that
Array Looks like:
let Deliveries=[{DeliveryID:"WHKYYY", OwnerID:2, Staus:"Delivered"},
{DeliveryID:"MHKYYY", OwnerID:3, Staus:"Delivered"},
{DeliveryID:"KKHKYYY", OwnerID:4, Staus:"Warehouse"},
{DeliveryID:"LLHKYYY", OwnerID:2, Staus:"Delivered"},
]
Problem is that when you are searching for something and it is not matching with the DeliveryID that time search result will be undefined. You can use || to avoid the undefined values to set the empty array like [] and before return the staus you can check the rearch result. If the result is empty return "".
Code like :
let track =data.Deliveries.filter(item=> Model.searchQuery===item.DeliveryID)||[];
if(track .length>0){
//mathch the ID and informaiton is avialable
//other code here
}else{
// does not match. return null or ""
}
The function bellow returns the first empty row on column A.
The sheet became full, I extended it with another 9000 rows, I ran main manually and I got an error, "TypeError: Cannot read property "0" from undefined".
The problem it seems to be that the 1001'th row cannot be read, values[1001] returns nothing, undefined. Am I missing something or am I limited to 1000 rows of data ?
Thank you for reading, here is the code:
function getLastRowNumber(sheetName){
// sheetName: string; returns intege (the last row number based on column A)
var sheet = getSheet(sheetName);
var column = sheet.getRange('A1:A'); // THIS IS THE PROBLEM
var values = column.getValues(); // get all data in one call
// DEBUG
Logger.log(values[1001][0])
var ct = 0;
while (values[ct][0] != "") {
ct++;
}
return (ct);
}
EDIT:
Solution: use .getRange('A:A'); instead of the 'A1:A' notation.
Thank you #tehhowch for the solution.
Posting this answer so people can see it, the solution was provided by #tehhowch.
By using "A:A" as the argument of getRange fixes the problem.
I use the Wordpress function $wpdb->get_results()
https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
It says:
"If no matching rows are found, or if there is a database error, the return value will be an empty array."
Then how can I know if the query failed OR if it's empty?
Use
$results=$wpdb->get_results($yoursql);
if (count($results)> 0){
//do here
}
But if you want to know if query failed
$wpdb -> show_errors ();
$wpdb -> get_results ($wpdb -> prepare($sql));
$wpdb -> print_error ();
Bit late to the party here but I'm just looking for the same thing. I've had a browse through the wp-db.php code on version 4.4.2.
On line 1422, inside the method flush() there's a bit of code which resets the last_error property:
$this->last_error = '';
This flush() method is called in the query() method on line 1693:
$this->flush();
The get_results() method calls query() on line 2322:
if ( $query ) {
$this->query( $query );
} else {
return null;
}
With this we can be pretty sure that more or less every time get_results() (Or get_row() too for that matter) is called, query() and flush() are both called, which ensures that last_error is set to the empty string before the query is executed.
So assuming the query runs (If it doesn't, null is returned - if the query is empty for example), last_error should contain an error message if the query was to fail for some reason.
Since last_error is flush()ed/reset each time, it should only contain an error for the last query that was run, rather than the last error for any query that had been run previously. With this in mind it should be safe to rely on last_error to determine whether something went wrong with the query.
$results = $wpdb->get_results($sql);
if (is_null($results) || !empty($wpdb->last_error)) {
// Query was empty or a database error occurred
} else {
// Query succeeded. $results could be an empty array here
}
Not the most intuitive in my opinion, but it seems to be sufficient.
Personally, I've written my own class around wpdb for my own benefit. This is my getResults() method.
public function getResults($query, $bindings = [])
{
// Prepare the statement (My prepare method inspects $query and just returns it if there's no bindings, otherwise it uses $wpdb->prepare()
$prepared = $this->prepare($query, $bindings);
// Execute the statement
$rows = $this->db->get_results($prepared, ARRAY_A);
// If an array was returned and no errors occurred, return the result set
if (is_array($rows) && empty($this->db->last_error)) {
return $rows;
}
// On failure, return false
return false;
}
Hope this helps.
Wpdb->get_results function from wordpress returns the result if successful otherwise it will return null. There can be many reasons if a query get failed.Refer in-depth article on debugging get_results() returning empty results here
Although you can use functions like wpdb->show_error() to check what was the last error after executing the sql query. sometimes this error returns empty
then try to use wpdb->last_query to check the final query that get formed.
Problem
Calling repeater('#myTable tr','Rows').count(); returns a Future, not an integer. I need to get the integer value so I can confirm that an additional row was added to a table.
Code
it('should add a new user when save button is clicked',function()
{
showModal();
//here I'm trynig to store the row count of my table into a local variable.
//a future is returned who's 'value' field is undefined.
var memberCount = repeater('#memberTable tr','Member Rows').count();
//this outputs 'undefined'
console.log(memberCount.value);
input('editedMember.name').enter('John');
input('editedMember.grade').enter(5);
input('editedMember.ladderPosition').enter(3);
element('#saveMemberButton').click();
sleep(1);
expect(element(modalId).css('display')).toBe('none');
//here is where I want to do the comparison against the above stored memberCount
expect(repeater('#memberTable tr', 'Member Rows').count()).toBe(memberCount.value + 1);
});
Test Result
Chrome 25.0 e2e should add a new user when save button is clicked FAILED
expect repeater 'Member Rows ( #memberTable tr )' count toBe null
/Users/jgordon/learning/chessClub/web-app/test/e2e/scenarios.js:45:3: expected null but was 6
Chrome 25.0: Executed 2 of 2 (1 FAILED) (1 min 4.117 secs / 1 min 3.773 secs)
Drilling into the source code for Angularjs' e2e support reveals that you have to call execute() on the Future to have it populate its value. Also, when you call execute you have to provide a "done" function to the execute() otherwise Testacular will (oddly enough!) skip your test.
Code
var rowCountFuture = repeater('#memberTable tr','Member Rows').count();
rowCountFuture.execute(function(){
});
var memberCount = rowCountFuture.value;
While I'm jazzed to see this works, I'm concerned there may be some asynchronous bugs that could come out of this, also, I feel like this is a hack and not the right way to do it. Any ideas?
Based on the latest Protractor version:
it('should add a new user when save button is clicked', function() {
var memberCount;
element.all(by.repeater('#memberTable tr','Member Rows')).count().then(function(value) {
memberCount = value;
});
...
// then do all your entering user info, saving etc.
...
browser.refresh(); // or however you want to load new data
expect(element.all(by.repeater('#memberTable tr','Member Rows')).count()).toEqual(memberCount + 1);
});
I've run into the same issue, and have seen confusing results when testing value returned after calling execute(). I've found this method works more reliably:
var getCount = repeater('ul li').count();
getCount.execute(function(value) {
expect(value).toEqual(3);
});
You can do this most easily in the async promise returned by the locator
element.all(By.repeater 'thing in things').then(function(elements){
count = elements.length;
expect(count).toEqual(3);
});
I have a field called force. Its by default a null field. I want to add 1 everytime I run an if block. here is my code sample
if($somecondition){
$array = array();
$array[] = $this->Model->read(null, 1);
$array['force']++;
$this->Model->updateAll(array('Model.complete' => 1, 'Model.force' => $array['force']),array('Model.completed IS NULL'));
}
I am getting an error of undefined variable $array. Not sure why.
it seems you are very new to Cake (and maybe even PHP)
if($somecondition){
$array = $this->Model->read('force',1);
if($array['Model']['force']===NULL)$array['Model']['force'] = 0;
$array['Model']['force']++;
$array['Model']['complete']=1;
$this->Model->save($array);
}
It you can, change the default value of 'force' to 0 in the DB, so you don't have to check for that here.
You should initialize $array['force'] to 0 before incrementing it.