REMINDER
Regster API Function.
-
This function I wrote does not always give me the token. (It does work sometimes)
This is the message I receive:"errorMessage": "Cannot read property '0' of undefined",
This is the fusction :
const cloudbackend = require('appdrag-cloudbackend'); cloudbackend.init(process.env.APIKEY, process.env.APPID); const bcrypt = require('bcrypt'); const uuid = require('uuid'); exports.handler = async(event, context, callback) => { let name = event["POST"]["name"]; let nickname = event["POST"]["nickname"]; let email = event["POST"]["email"]; let password = event["POST"]["password"]; let hachPassword = bcrypt.hashSync(password, 10); //Check email does not exists. let finedEmai = `SELECT email FROM users WHERE email = '${email}' ` await cloudbackend.sqlSelect(finedEmai).then(async res => { let result = JSON.parse(res); if (result.Table == null) { // Add user to DB. let sql = `INSERT INTO users (name, nickname, email, password, date_joined, token) VALUES ('${name}', '${nickname}', '${email}','${hachPassword}', now() , uuid())`; await cloudbackend.sqlExecuteRawQuery(sql).then(async res => { // Get current users token let cotentToken = `SELECT token FROM users WHERE id = LAST_INSERT_ID()` await cloudbackend.sqlSelect(cotentToken).then(async res => { let result = await JSON.parse(res); let token = result.Table[0] callback(null, token); }); }); } else { callback(null, result.Table[0]) } }) }
-
Hi,
You can simplify the process- Set the email column as a unique in the table edition
- Then check how many rows have been affected in the return of sqlExecuteRawQuery
- If it's 0 throw an error, if it's 1 select the token of the new user
By the way avoid nesting async await.. It adds complexity for no reason.
-
Hi!
Thanks for you answer!
I tried setting the email column as unique and this it the error I am getting:Error: Specified key was too long; max key length is 767 bytes
{"m_MaxCapacity":2147483647,"Capacity":1768,"m_StringValue":"ALTER TABLE
my-recipe-app-1fe491
.users
CHANGE COLUMNid
id
int AUTO_INCREMENT COMMENT '::' FIRST ;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNname
name
varchar(255) COMMENT '::' AFTERid
;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNnickname
nickname
varchar(255) COMMENT '::' AFTERname
;
ALTER TABLEmy-recipe-app-1fe491
.users
ADD UNIQUE INDEXemail_UNIQUE
(email
ASC);
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNemail
email
varchar(200) COMMENT 'email::' AFTERnickname
;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNpassword
password
varchar(255) COMMENT 'password::' AFTERemail
;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNdate_joined
date_joined
date COMMENT '::' AFTERpassword
;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNactive
active
int DEFAULT '0' COMMENT '::' AFTERdate_joined
;
ALTER TABLEmy-recipe-app-1fe491
.users
CHANGE COLUMNtoken
token
varchar(255) COMMENT '::' AFTERactive
;
","m_currentThread":0} -
@malky-shlomowith said in Regster API Function.:
my-recipe-app-1fe491
It seems that for unique column the length should be smaller, try 150 or 100 instead of 200 for the email. There won't be a single user with an email address that long.
-
thanks for your help!
-
you're welcome !
-
@Wassim said in Regster API Function.:
how many rows have been affected in the return of sqlExecuteRawQuery
how do I check it?
-
https://www.npmjs.com/package/appdrag-cloudbackend
You can JSON.parse(response); and check numberOfAffectedRows.