Object(...) is not a function on index.js when use firestore it give me error - react-redux-firebase

14 | const store = createStore(
15 | rootReducer,
16 | compose(
17 | applyMiddleware(thunk.withExtraArgument({ getFirebase, getFirestore })),
18 | reduxFirestore(fbConfig),
19 | reactReduxFirestore(fbConfig)
20 | )enter image description here

Related

How do you import the command mysqlx into my React App?

I'm trying to import the command mysqlx into my React App.
I tried a couple of different methods but it does not recognize the command mysqlx in the console. It seems I can't call it without importing it into the app. I'm pretty new at this so it may be something easy. This is all on a local windows machine.
import mysqlx from '#mysql/xdevapi'
After I tried to run the app. See error below.
TypeError: Cannot read property 'toString' of undefined
(anonymous function)
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/lib/Protocol/Util/systemAttributes.js:101
98 | }
99 |
100 | const clientAttributes = {
> 101 | _pid: process.pid.toString(),
102 | _platform: platformName(),
103 | _os: osName(),
104 | _source_host: sourceHostName(),
View compiled
./node_modules/#mysql/xdevapi/lib/Protocol/Util/systemAttributes.js
http://localhost:3000/static/js/0.chunk.js:44169:30
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
▼ 2 stack frames were expanded.
(anonymous function)
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/lib/Protocol/Client.js:68
./node_modules/#mysql/xdevapi/lib/Protocol/Client.js
http://localhost:3000/static/js/0.chunk.js:22283:30
▲ 2 stack frames were expanded.
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
./node_modules/#mysql/xdevapi/lib/DevAPI/Session.js
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/lib/DevAPI/Session.js:32
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
./node_modules/#mysql/xdevapi/lib/DevAPI/ConnectionPool.js
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/lib/DevAPI/ConnectionPool.js:32
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
./node_modules/#mysql/xdevapi/lib/DevAPI/Client.js
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/lib/DevAPI/Client.js:32
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
./node_modules/#mysql/xdevapi/index.js
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/node_modules/#mysql/xdevapi/index.js:36
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
Module../src/App.js
http://localhost:3000/static/js/main.chunk.js:91:72
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
Module../src/index.js
http://localhost:3000/static/js/main.chunk.js:220:62
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
fn
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:150
147 | );
148 | hotCurrentParents = [];
149 | }
> 150 | return __webpack_require__(request);
| ^ 151 | };
152 | var ObjectFactory = function ObjectFactory(name) {
153 | return {
View compiled
0
http://localhost:3000/static/js/main.chunk.js:359:18
__webpack_require__
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:785
782 | };
783 |
784 | // Execute the module function
> 785 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
| ^ 786 |
787 | // Flag the module as loaded
788 | module.l = true;
View compiled
checkDeferredModules
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:45
42 | }
43 | if(fulfilled) {
44 | deferredModules.splice(i--, 1);
> 45 | result = __webpack_require__(__webpack_require__.s = deferredModule[0]);
| ^ 46 | }
47 | }
48 |
View compiled
Array.webpackJsonpCallback [as push]
C:/Users/brian/OneDrive/Desktop/nms-appV4/nms-app/webpack/bootstrap:32
29 | deferredModules.push.apply(deferredModules, executeModules || []);
30 |
31 | // run deferred modules when all chunks ready
> 32 | return checkDeferredModules();
| ^ 33 | };
34 | function checkDeferredModules() {
35 | var result;
View compiled
(anonymous function)
http://localhost:3000/static/js/main.chunk.js:1:67
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error.
There isn't much info presented here, but based on your error:
process.pid is undefined. You have to figure out why that is undefined. There are similar questions:
Node console.log(process.pid) prints undefined
I don't understand if this is something running with Electron or a plain browser web app, in any case, you don't seem to be running Node.js, but some other JS environment which does have the process API. If it's Electron, then using node as the target, as described in the answer linked by the first answer, should do the trick. If it's a plain browser web app, it won't work.
I figured it out, I used express to create a new route and was able to get the database connected via route.use() function.
For a follow up question please visit:
How to connect my route parameter in express with #mysql/x devAPI?

MySQL SUM returns unexpected value

I've been trying to use SUM() to total up some numeric values in my database, but it seems to be returning unexpected values. Here is the PHP-side for generating the SQL:
public function calcField(string $field, array $weeks)
{
$stmt = $this->conn->prepare('SELECT SUM(`'. $field .'`) AS r FROM `ws` WHERE `week` IN(?);');
$stmt->execute([implode(',', $weeks)]);
return $stmt->fetch(\PDO::FETCH_ASSOC)['r'];
}
Let's give it some example data for you folks at home:
$field = 'revenue';
$weeks = [14,15,16,17,18,19,20,21,22,23,24,25,26];
This returns this value:
4707.92
Without seeing the data, this may have seemed to have worked, but here's the rows for those weeks:
+----+------+------+---------+-------+---------+---------+------+----------+
| id | week | year | revenue | sales | gpm_ave | uploads | pool | sold_ave |
+----+------+------+---------+-------+---------+---------+------+----------+
| 2 | 14 | 2019 | 4707.92 | 292 | 13 | 0 | 1479 | 20 |
| 3 | 15 | 2019 | 4373.32 | 304 | 13 | 0 | 1578 | 19 |
| 4 | 16 | 2019 | 4513.10 | 275 | 14 | 0 | 1460 | 19 |
| 5 | 17 | 2019 | 4944.80 | 336 | 14 | 0 | 1642 | 20 |
| 6 | 18 | 2019 | 4343.87 | 339 | 13 | 0 | 1652 | 21 |
| 7 | 19 | 2019 | 3918.59 | 356 | 14 | 0 | 1419 | 25 |
| 8 | 20 | 2019 | 4091.20 | 247 | 19 | 0 | 1602 | 15 |
| 9 | 21 | 2019 | 4177.22 | 242 | 12 | 0 | 1588 | 15 |
| 10 | 22 | 2019 | 3447.88 | 227 | 18 | 0 | 1585 | 14 |
| 11 | 23 | 2019 | 3334.18 | 216 | 15 | 0 | 1675 | 13 |
| 12 | 24 | 2019 | 4736.15 | 281 | 13 | 0 | 1388 | 20 |
| 13 | 25 | 2019 | 4863.84 | 252 | 12 | 0 | 1465 | 17 |
| 14 | 26 | 2019 | 4465.95 | 281 | 21 | 0 | 1704 | 16 |
+----+------+------+---------+-------+---------+---------+------+----------+
As you can see, the total should be far greater than 4707.92 - and I notice that the first row revenue = 4707.92.
Here's what things get weird, if I add this into the function:
echo 'SELECT SUM(`'. $field .'`) AS r FROM `ws` WHERE `week` IN('. implode(',', $weeks) .');';
Which outputs:
SELECT SUM(revenue) AS r FROM ws WHERE week IN(14,15,16,17,18,19,20,21,22,23,24,25,26);
Copying and pasting this into MySQL CLI returns:
MariaDB [nmn]> SELECT SUM(revenue) AS r FROM ws WHERE week IN(14,15,16,17,18,19,20,21,22,23,24,25,26);
+----------+
| r |
+----------+
| 55918.02 |
+----------+
1 row in set (0.00 sec)
Which, looks a lot more accurate. However, that very same SQL statement returns the first row value rather than summing the column for those weeks.
This function gets triggered by an AJAX script:
$d = new Page\Snapshot\D();
# at the minute only outputting dump of values to see what happens
echo '<pre>'. print_r(
$d->getQuarterlySnapshot(new Page\Snapshot\S(), new App\Core\Date(), $_POST['quarter'], '2019'),
1
). '</pre>';
The function $d->getQuarterlySnapshot function:
public function getQuarterlySnapshot(S $s, Date $date, int $q, string $year)
{
switch($q)
{
case 1:
$start = $year. '-01-01 00:00:00';
$end = $year. '-03-31 23:59:59';
break;
case 2:
$start = $year. '-04-01 00:00:00';
$end = $year. '-06-30 23:59:59';
break;
case 3:
$start = $year. '-07-01 00:00:00';
$end = $year. '-09-30 23:59:59';
break;
case 4:
$start = $year. '-10-01 00:00:00';
$end = $year. '-12-31 23:59:59';
break;
}
$weeks = $date->getWeeksInRange('2019', 'W', $start, $end);
foreach ($weeks as $key => $week){$weeks[$key] = $week[0];}
return [
'rev' => $s->calcField('revenue', $weeks),
'sales' => $s->calcField('sales', $weeks),
'gpm_ave' => $s->calcField('gpm_ave', $weeks),
'ul' => $s->calcField('uploads', $weeks),
'pool' => $s->calcField('pool', $weeks),
'sold_ave' => $s->calcField('sold_ave', $weeks)
];
}
So I don't overwrite the value anywhere (that I can see at least). How do I use SUM() with the IN() conditional?
As pointed out in the comments by #MadhurBhaiya:
Comma separated week_id values is passing as a single string in the query: week_id in ('1,2,3...,15') . MySQL implicitly typecasts this to 1 and thus gets the first row only. You will need to change the query preparation code
Led me to breaking up the single ? into named parameters using a foreach loop:
public function calcField(string $field, array $weeks)
{
$data = [];
$endKey = end(array_keys($weeks));
$sql = 'SELECT SUM(`'. $field .'`) AS r FROM `ws` WHERE `week` IN(';
foreach ($weeks as $key => $week)
{
$sql .= ':field'. $key;
$sql .= ($key !== $endKey ? ',' : '');
$data[':field'. $key] = $week;
}
$sql .= ');';
$stmt = $this->conn->prepare($sql);
$stmt->execute($data);
return $stmt->fetch(\PDO::FETCH_ASSOC)['r'];
}
Now each numeric value is treated individually, which, is getting me the expected value.
Yes, it is showing only first row data in your query because you have mentioned where week IN (1,2,3,4,5,6,7,8,9,10,11,12,13); and your data shows that you have week 13 and 1 to 12 does not exist in your data from IN clause of your query.
You are selecting only the row with week 13, the week 1-12 is not existing in your data. That's why the result of your query is 43.900001525878906
Solution 1:
You might want to change the values in your IN()because you are filtering your data by the column week.
SELECT SUM(`revenue`) as `r` FROM `ws` WHERE `week` IN (13,14,15,16,17,18,19,20,21,22,23,24,25);
Solution 2:
You can change the week in your where clause to id
SELECT SUM(`revenue`) as `r` FROM `ws` WHERE `id` IN (1,2,3,4,5,6,7,8,9,10,11,12,13);

Adding one hour to datetime and comparing with present time based on hours and minutes

I have written a query where I am adding one hour to a datetime which is present in database, and comparing that time to the present time based on minutes and hours.
Table structure of upgrades:
id| postid |type |status| counter| datetime | autoreposttime
1 | 139 | M | P | 1 | 2017-04-26 10:49:23 | 60
2 | 140 | M | P | 1 | 2017-04-26 10:49:27 | 60
3 | 141 | M | P | 1 | 2017-04-26 10:49:31 | 60
4 | 142 | M | P | 1 | 2017-04-26 10:49:34 | 60
Table structure of posts:
post_id | locationid | priority_time
81 | 1 | 2017-04-20 18:29:17
82 | 27 | 2017-04-20 18:29:19
85 | 27 | 2017-04-20 18:29:07
Here is my SQL query which I have written in where I want to retrieve rows where datetime + 1 hour is equal to present time.
$posts = DB::table('upgrades')
->join('posts','posts.post_id','=','upgrades.postid')
->where(DB::raw('DATE_FORMAT(DATE_ADD(`upgrades`.`datetime`, INTERVAL 1 HOUR),"%Y-%m-%d %H:%i")'),$datetime)
->select('posts.*','upgrades.*')->get();
I'm getting data as null, and I think there's some problem in the where condition. What is wrong with my query?
One way to do that is to use Carbon. If I understood you correctly, you want result for specified minute, so here's an example:
$time = Carbon::now()->subHour();
$posts = ....
->whereBetween('datetime', [$time->format('Y-m-d H:i:00'), $time->format('Y-m-d H:i:59'])
....
->get();

eloquent with Order by not returning correctly the SUM of field

I'm trying to work with eloquents and I'm having problems in generating the expected result.
To be honest, I will show you what I have, what I want and what I did. But, maybe there is another way to do what I want and I will be happy to test it as well. :)
What I have:
I do have a table, that is:
+----+------------+--------------+---------------+---------------------+---------------------+
| id | machine_id | balance_left | balance_found | created_at | updated_at |
+----+------------+--------------+---------------+---------------------+---------------------+
| 1 | 1 | 12.30 | 12.30 | 2016-01-26 16:39:18 | 2016-01-28 16:39:18 |
| 2 | 2 | 45.60 | 45.60 | 2016-01-26 16:39:18 | 2016-01-28 16:39:18 |
| 3 | 3 | 78.90 | 78.90 | 2016-01-26 16:40:22 | 2016-01-28 16:40:22 |
| 4 | 4 | 90.12 | 90.12 | 2016-01-26 16:40:22 | 2016-01-28 16:40:22 |
| 5 | 1 | 5.60 | 3.40 | 2016-01-28 17:30:33 | 2016-01-28 17:30:33 |
| 6 | 2 | 7.89 | 4.56 | 2016-01-28 17:30:33 | 2016-01-28 17:30:33 |
+----+------------+--------------+---------------+---------------------+---------------------+
What I need:
Given a date, I do need to get the newest results and SUM the 'balance_left'. BUT, having the machine_id as unique entry.
For example, if I pass as date 26/01 at 23:59, I would expect to receive the lines with ID 1, 2, 3 and 4. And, the SUM would be 12.30 + 45.60 + 78.90 + 90.12 = 226.92.
Now, if I pass as date 28/01 at 23:59, I would expect to receive the lines with ID 3, 4, 5 and 6. Once that the entries 5 and 6 are the NEWEST entries for the machine_id 1 and 2. In this case, the sum would be: 78.90 +90.12 + 5.60 + 7.89 = 182.51
What I did:
$sub = App\Transaction::orderBy('created_at','DESC');
$transcations = DB::table(DB::raw("({$sub->toSql()}) as sub"))->where('created_at','<=', '2016-01-28 23:59:59')->groupBy('machine_id')->sum('balance_left');
Without the SUM, but using a select('balance_left'), I'm getting the correct lines:
=> [
{#672
+"balance_left": 5.6,
},
{#671
+"balance_left": 7.89,
},
{#643
+"balance_left": 78.9,
},
{#677
+"balance_left": 90.12,
},
]
The problem is that when I request the sum, I'm getting:
=> 17.9
And, after a while, I discovered that actually, he is doing a sum of the machine_id entry, so the lines with ID 1 and 5, have machine_id 1, and then this sum is the sum of 12.30 +5.60 = 17.90
Problem:
How can I actually SUM the lines returned? Or, is there any other better way to return what I'm expecting?
Thanks a lot!

Which internal time representation from MySQL is this?

This is a snap of my current project MySQL console:
+---------------------+-----------+-----------+
| date_time_local | delta_kWh | prod_hour |
+---------------------+-----------+-----------+
| 2014-05-25 05:20:00 | 0.169 | 5 |
| 2014-05-25 06:00:00 | 0.751 | 6 |
| 2014-05-25 07:00:00 | 0.743 | 7 |
| 2014-05-25 08:00:00 | 0.830 | 8 |
+---------------------+-----------+-----------+
MySQL shows the prod hour as a number but in the result recived by the client the corresponding values are:
[{"date_time_local":"2014-05-25 05:20:00","delta_kWh":"0.169","prod_hour":504403295704449000},
{"date_time_local":"2014-05-25 06:00:00","delta_kWh":"0.751","prod_hour":576460889742377000},
{"date_time_local":"2014-05-25 07:00:00","delta_kWh":"0.743","prod_hour":648518483780304900},
{"date_time_local":"2014-05-25 08:00:00","delta_kWh":"0.830","prod_hour":720576077818232800}]
What is the prod hour returned by the server?
My MySQL query is:
var resQuery = "SELECT concat(date_time_local) AS date_time_local, SUM(delta_kWh) AS delta_kWh, EXTRACT(HOUR FROM date_time_local) AS prod_hour FROM local_solar_consumption WHERE date_time_local BETWEEN '" + jsonRequestObj.startdate + "' AND '" + jsonRequestObj.enddate + "' GROUP BY prod_hour";
Edit:
I send the result array with:
that.resultsToSocket(resQuery, "ownProdCons", plotcase, socket);
rows.push(plotcase);
rows.push(plottype);
socket.emit('got_data', rows);
and stringify the result on the client side:
socket.on('got_data', function (data) {
em.plotCharts(data);
console.log(JSON.stringify(data));
//alert(JSON.stringify(data));
});