Getting node API up and running - node.js

I am attempting to follow this tutorial on using using node, Express, and postgresql to build and API. All my code for this can be found on Github.
In my server/routes/index.js is the following (the numbering being line numbers).
1: app.get('/api/todos', todosController.list);
2: // app.post('/api/todos/:todoId/items', todoItemsController.create);
3: app.post('/api/todos', todosController.create);
4: app.get('/api/todos/:todoId', todosController.retrieve);
5: app.put('/api/todos/:todoId', todosController.update);
6: app.delete('/api/todos/:todoId', todosController.destroy);
I am having two problems.
The first problem is lines 4 and 5 don't work (all of the other lines do minus line 2). When I use postman/browser to test them I get this error
ReferenceError: TodoItem is not defined
at retrieve (/Users/X-lem/Development/TestAPI/server/controllers/todos.js:25:20)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:281:22
at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:354:14)
at param (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:410:3)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
at urlencodedParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/urlencoded.js:91:7)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
at jsonParser (/Users/X-lem/Development/TestAPI/node_modules/body-parser/lib/types/json.js:109:7)
at Layer.handle [as handle_request] (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:317:13)
at /Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:335:12)
at next (/Users/X-lem/Development/TestAPI/node_modules/express/lib/router/index.js:275:10)
Secondly, when I uncomment line 2 the console crashes and I get this error.
ReferenceError: todoItemsController is not defined
at module.exports (/Users/X-lem/Development/TestAPI/server/routes/index.js:9:40)
at Object.<anonymous> (/Users/X-lem/Development/TestAPI/app.js:16:27)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Module.require (module.js:556:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/X-lem/Development/TestAPI/bin/www:3:13)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Function.Module.runMain (module.js:653:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
[nodemon] app crashed - waiting for file changes before starting...
I am new to building API's and am trying to teach myself how to do it. I have tried a number of different troubleshooting methods with no avail. Any help is appreciated, thank you.

Hope I can help a little bit. Your problem #2 was because you did mistype. Please, take a look at your routes/index.js file. You imported controllers to the variable todosController, but on the 9-th line you are using not defined variable todoItemsController. So, just
app.post('/api/todos/:todoId/items', todosController.create); will work.

Related

auth operation not supported in this environment

I am trying to build a google authentication in signup/login using firebase in my express with handlebar application but on clicking button & doing the pre-requisites as per firebase-auth docs it is giving me the error as follows:
auth/operation-not-supported-in-this-environment
to be precise as follows :
FirebaseError: Firebase: Error (auth/operation-not-supported-in-this-environment).
at createErrorInternal (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:509:47)
at _createError (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:485:32)
at Object. (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index-69e083b1.js:7273:27)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object. (D:\web developement\myna\sample project\node_modules#firebase\auth\dist\node\index.js:5:13) {
code: 'auth/operation-not-supported-in-this-environment',
customData: {}
}
i am not getting a clue, what is causing it & in order to solve it what should i do

AdapterError: Unexpected error from database adapter: `select` failed ("badConnection")

I'm getting the following error if I run any api end point where db operation is involved in sails.js (v 1.4.0). I'm using sails-mysql (v 1.0.1).
For your information, it is producing this error in aws ec2 ubuntu 18. My node version 13/14.
I'm not getting any error in my development environment.
error: Sending 500 ("Server Error") response:
AdapterError: Unexpected error from database adapter: `select` failed ("badConnection"). A connection either could not be obtained or there was an error using the connection.
Additional data:
{
error: Error: getaddrinfo EAI_AGAIN abmaster
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26)
--------------------
at Protocol._enqueue (/var/www/html/api/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/var/www/html/api/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at PoolConnection.connect (/var/www/html/api/node_modules/mysql/lib/Connection.js:130:18)
at Pool.getConnection (/var/www/html/api/node_modules/mysql/lib/Pool.js:48:16)
at Object.getConnection (/var/www/html/api/node_modules/machinepack-mysql/lib/get-connection.js:78:25)
at wrapper (/var/www/html/api/node_modules/#sailshq/lodash/lib/index.js:3282:19)
at Deferred.parley.retry [as _handleExec] (/var/www/html/api/node_modules/machine/lib/private/help-build-machine.js:1076:19)
at Deferred.exec (/var/www/html/api/node_modules/parley/lib/private/Deferred.js:286:10)
at Deferred.switch (/var/www/html/api/node_modules/machine/lib/private/help-build-machine.js:1469:16)
at spawnConnection (/var/www/html/api/node_modules/sails-mysql/helpers/private/connection/spawn-connection.js:29:10)
at Object.spawnOrLeaseConnection (/var/www/html/api/node_modules/sails-mysql/helpers/private/connection/spawn-or-lease-connection.js:23:3)
at Object.select (/var/www/html/api/node_modules/sails-mysql/helpers/select.js:122:24)
at wrapper (/var/www/html/api/node_modules/#sailshq/lodash/lib/index.js:3282:19)
at Deferred.parley.retry [as _handleExec] (/var/www/html/api/node_modules/machine/lib/private/help-build-machine.js:1076:19)
at Deferred.exec (/var/www/html/api/node_modules/parley/lib/private/Deferred.js:286:10)
at Deferred.switch (/var/www/html/api/node_modules/machine/lib/private/help-build-machine.js:1469:16)
at Object.find (/var/www/html/api/node_modules/sails-mysql/lib/adapter.js:185:16)
at _getPopulatedRecords (/var/www/html/api/node_modules/waterline/lib/waterline/utils/query/help-find.js:145:21)
at helpFind (/var/www/html/api/node_modules/waterline/lib/waterline/utils/query/help-find.js:582:6)
at _afterPotentiallyRunningBeforeLC (/var/www/html/api/node_modules/waterline/lib/waterline/methods/find-one.js:265:9)
at _maybeRunBeforeLC (/var/www/html/api/node_modules/waterline/lib/waterline/methods/find-one.js:239:16)
at Deferred._.extend._WLModel [as _handleExec] (/var/www/html/api/node_modules/waterline/lib/waterline/methods/find-one.js:241:9)
at Deferred.exec (/var/www/html/api/node_modules/parley/lib/private/Deferred.js:286:10)
at Object.findOneRecord (/var/www/html/api/node_modules/sails/lib/hooks/blueprints/actions/findOne.js:37:4)
at /var/www/html/api/node_modules/sails/lib/router/bind.js:248:46
at routeTargetFnWrapper (/var/www/html/api/node_modules/sails/lib/router/bind.js:395:9)
at /var/www/html/api/node_modules/sails/lib/router/bind.js:458:14
at Layer.handle [as handle_request] (/var/www/html/api/node_modules/express/lib/router/layer.js:95:5) {
errno: -3001,
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'abmaster',
fatal: true
},
meta: {
host: 'test.anonderbazar.com',
port: 3306,
schema: true,
adapter: 'sails-mysql',
url: 'mysql://abmaster:duY$1L#8rwof#test.anonderbazar.com:3306/anonderbazaar',
connectTimeout: 20000,
identity: 'default'
}
}
at Object.findOneRecord (/var/www/html/api/node_modules/sails/lib/hooks/blueprints/actions/findOne.js:36:4)
at /var/www/html/api/node_modules/sails/lib/router/bind.js:248:46
at routeTargetFnWrapper (/var/www/html/api/node_modules/sails/lib/router/bind.js:395:9)
at /var/www/html/api/node_modules/sails/lib/router/bind.js:458:14
at Layer.handle [as handle_request] (/var/www/html/api/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/html/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/html/api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/html/api/node_modules/express/lib/router/layer.js:95:5)
at /var/www/html/api/node_modules/express/lib/router/index.js:281:22
at param (/var/www/html/api/node_modules/express/lib/router/index.js:354:14)
at param (/var/www/html/api/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/var/www/html/api/node_modules/express/lib/router/index.js:410:3)
at next (/var/www/html/api/node_modules/express/lib/router/index.js:275:10)
at next (/var/www/html/api/node_modules/express/lib/router/route.js:127:14)
at alwaysAllow (/var/www/html/api/node_modules/sails/lib/hooks/policies/index.js:178:16)
at routeTargetFnWrapper (/var/www/html/api/node_modules/sails/lib/router/bind.js:395:9)
at /var/www/html/api/node_modules/sails/lib/router/bind.js:458:14
at Layer.handle [as handle_request] (/var/www/html/api/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/html/api/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/html/api/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/html/api/node_modules/express/lib/router/layer.js:95:5)
at /var/www/html/api/node_modules/express/lib/router/index.js:281:22
at param (/var/www/html/api/node_modules/express/lib/router/index.js:354:14)
at param (/var/www/html/api/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/var/www/html/api/node_modules/express/lib/router/index.js:410:3)
at next (/var/www/html/api/node_modules/express/lib/router/index.js:275:10)
at next (/var/www/html/api/node_modules/express/lib/router/route.js:127:14)
at Hook.expressMiddleware (/var/www/html/api/node_modules/sails/lib/hooks/i18n/index.js:205:14)
Any kind of help from anyone will be much appreciated.
I had this problem, my problem was that the password of my user had an '#' and in the connection there was also one, change the password and solve the error

Node app not sending any data while login 'ER_NO_DEFAULT_FOR_FIELD',

I am transferring a node app from one server to another using different domain name.
I have setup everything but I cannot login to the control panel of the app which I have created, I am getting the following error.
{ Error: ER_NO_DEFAULT_FOR_FIELD: Field 'origin' doesn't have a default value
at Query.Sequence._packetToError (/var/www/node/node_modules/mysql/lib/pro tocol/sequences/Sequence.js:47:14)
at Query.ErrorPacket (/var/www/node/node_modules/mysql/lib/protocol/sequen ces/Query.js:77:18)
at Protocol._parsePacket (/var/www/node/node_modules/mysql/lib/protocol/Pr otocol.js:278:23)
at Parser.write (/var/www/node/node_modules/mysql/lib/protocol/Parser.js:7 6:12)
at Protocol.write (/var/www/node/node_modules/mysql/lib/protocol/Protocol. js:38:16)
at Socket.<anonymous> (/var/www/node/node_modules/mysql/lib/Connection.js: 91:28)
at Socket.<anonymous> (/var/www/node/node_modules/mysql/lib/Connection.js: 502:10)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
--------------------
at Protocol._enqueue (/var/www/node/anode_modules/mysql/lib/protocol/Protoc ol.js:144:48)
at Connection.query (/var/www/node/node_modules/mysql/lib/Connection.js:20 0:25)
at Object.managerLogin (/var/www/node/node_modules/login/index.js:58:15)
at callbackFunction (/var/www/node/routes/index.js:62:16)
at Layer.handle [as handle_request] (/var/www/node/node_modules/express/lib/router/ layer.js:95:5)
at next (/var/www/node/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/var/www/node/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/node/node_modules/express/lib/router/ layer.js:95:5)
at /var/www/node/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/var/www/node/node_modules/express/lib/router/index.js: 330:12)
at next (/var/www/node/node_modules/express/lib/router/index.js:271:10)
at /var/www/node/app.js:55:7
at Layer.handle [as handle_request] (/var/www/node/node_modules/express/lib/router/ layer.js:95:5)
at trim_prefix (/var/www/node/node_modules/express/lib/router/index.js:312:13)
at /var/www/node/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/var/www/node/node_modules/express/lib/router/index.js: 330:12)
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlMessage: 'Field \'origin\' doesn\'t have a default value',
sqlState: 'HY000',
index: 0,
sql:
'INSERT INTO userLog SET userId = 2, token = \'9d99a1a0-7350-11e9-9671-95abec1f75f6\', ip = \'\', browser = \'\', isLog = \'y\', isValid = \'y\'' }
Cannot figure this out what is happening,app was running file in the old server.
Debugged it, found it's a mysql bug, edited etc/mysql/my.cnf with [mysqld]
port = 3306
sql-mode=""
reloaded mysql, and it worked for me!

node- Some file references are missing in stack trace with async/await

Stacktrace is not having the reference to actual file name from where the error is triggered for the protractor.
Spec reporter configuration:
jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true,}}));
Sample spec:
it('Protractor stacktrce issue', async () =>
{
const helper = new SearchHelper();
await helper.doSearch('test');
});
search-helper.ts:
async doSearch(text) {
await browser.get('http://google.com');
await browser.wait(EC.visibilityOf(element(by.name('q1'))), 10000, 'No Element found');
await element(by.name('q1')).click(); //invalid locator
}
1. Stack Trace with browser.wait
✗ Protractor stack trace issue
-Failed: No Element found Wait timed out after 10007ms
Wait timed out after 10007ms
at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
at TimeoutError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:238:5)
at /usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2107:17
at process._tickCallback (internal/process/next_tick.js:109:7)
From asynchronous test: Error at Suite.<anonymous> (/Users/e2e/sample-spec.ts:27:3)
at Object.<anonymous> (/Users/e2e/sample-spec.ts:15:1)
at Module._compile (module.js:570:32) at Module.m._compile (/Users/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/node_modules/ts-node/src/index.ts:395:12)
2. If we comment browser.wait statement then the stacktrace is
✗ Protractor stack trace issue
Failed: No element found using locator: By(css selector, *[name="q1"])
at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
at NoSuchElementError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:168:5)
at elementArrayFinder.getWebElements.then (/usr/local/lib/node_modules/protractor/lib/element.ts:851:17)
at process._tickCallback (internal/process/next_tick.js:109:7)Error
at ElementArrayFinder.applyAction_ (/usr/local/lib/node_modules/protractor/lib/element.ts:482:23)
at ElementArrayFinder.(anonymous function) [as click] (/usr/local/lib/node_modules/protractor/lib/element.ts:96:21)
at ElementFinder.(anonymous function) [as click] (/usr/local/lib/node_modules/protractor/lib/element.ts:873:14)
at SearchHelper.<anonymous> (/Users/e2e/search-helper.ts:14:34) **at step (/Users/e2e/search-helper.ts:32:23)
at Object.next (/Users/e2e/search-heper.ts:13:53)
at fulfilled (/Users/e2e/search-helper.ts:4:58)**
at process._tickCallback (internal/process/next_tick.js:109:7)
From asynchronous test: Error at Suite.<anonymous> (/Users/e2e/sample-spec.ts:27:3)
at Object.<anonymous> (/Users/e2e/sample-spec.ts:15:1) at Module._compile (module.js:570:32)
at Module.m._compile (/Users/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/node_modules/ts-node/src/index.ts:395:12)
Observe the 2nd stack trace, it has references to search-helper file but it's not there in the first stack trace. It's very important to have this reference in stack trace for debugging.

selenium WebDriverError: connection refused nodejs

node version: v8.5.0
npm version: 5.3.0
geckodriver: v0.19.0
"selenium-webdriver": "^3.5.0"
I'm trying to test selenium webdriver in nodejs, however I keep getting the following error:
/home/app_path/node_modules/selenium-webdriver/lib/promise.js:2634
throw error;
^
WebDriverError: connection refused
at Object.throwDecodedError (/home/app_path/node_modules/selenium-webdriver/lib/error.js:497:15)
at parseHttpResponse (/home/app_path/node_modules/selenium-webdriver/lib/http.js:519:13)
at doSend.then.response (/home/app_path/node_modules/selenium-webdriver/lib/http.js:441:30)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
From: Task: WebDriver.createSession()
at Function.createSession (/home/app_path/node_modules/selenium-webdriver/lib/webdriver.js:769:24)
at Function.createSession (/home/app_path/node_modules/selenium-webdriver/firefox/index.js:572:41)
at createDriver (/home/app_path/node_modules/selenium-webdriver/index.js:170:33)
at Builder.build (/home/app_path/node_modules/selenium-webdriver/index.js:645:16)
at Object.<anonymous> (/home/app_path/app.js:20:2)
at Module._compile (module.js:624:30)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:201:16)
at bootstrap_node.js:626:3
A Mozilla firefox window just opens, and after some time, this error appears in console. This is what I'm trying to do:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build().then(function(){
console.log("log");
driver.get("https://google.co.in");
});
how to fix this?
EDIT Here's the stacktrace
{"value":{"error":"unknown error","message":"connection refused","stacktrace":"stack backtrace:\n 0: 0x4f388c - backtrace::backtrace::trace::h736111741fa0878e\n 1: 0x4f38c2 - backtrace::capture::Backtrace::new::h63b8a5c0787510c9\n 2: 0x442d88 - webdriver::error::WebDriverError::new::hea6d4dbf778b2b24\n 3: 0x449f99 - geckodriver::marionette::MarionetteHandler::create_connection::hf8b6061dba65cdd0\n 4: 0x42ac91 - >::run::hba9181b5aacf8f04\n 5: 0x402c59 - std::sys_common::backtrace::__rust_begin_short_backtrace::h19de262639927233\n 6: 0x40c065 - std::panicking::try::do_call::h6c1659fc4d01af51\n 7: 0x5e38ec - panic_unwind::__rust_maybe_catch_panic\n at /checkout/src/libpanic_unwind/lib.rs:98\n 8: 0x420d32 - >::call_box::h953e5f59694972c5\n 9: 0x5dc00b - alloc::boxed::{{impl}}::call_once<(),()>\n at /checkout/src/liballoc/boxed.rs:661\n - std::sys_common::thread::start_thread\n at /checkout/src/libstd/sys_common/thread.rs:21\n - std::sys::imp::thread::{{impl}}::new::thread_start\n at /checkout/src/libstd/sys/unix/thread.rs:84"}}

Resources