Navigation

    APPDRAG Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular

    REMINDER

    Please be respectful of all AppDragers! Keep it really civil so that we can make the AppDrag community of builders as embracing, positive and inspiring as possible.

    Regster API Function.

    Cloud Backend (Cloud DB, API Builder)
    2
    8
    447
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      malky shlomowith last edited by malky shlomowith

      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])
              }
          })
      }
      
      1 Reply Last reply Reply Quote 0
      • Wassim
        Wassim last edited by

        Hi,
        You can simplify the process

        • Set the email column as a unique in the table edition

        ba42a61c-bf07-49f7-94a3-ebfeb360cfe3-image.png

        • 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.

        1 Reply Last reply Reply Quote 0
        • M
          malky shlomowith last edited by

          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 COLUMN id id int AUTO_INCREMENT COMMENT '::' FIRST ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN name name varchar(255) COMMENT '::' AFTER id ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN nickname nickname varchar(255) COMMENT '::' AFTER name ;
          ALTER TABLE my-recipe-app-1fe491.users ADD UNIQUE INDEX email_UNIQUE (email ASC);
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN email email varchar(200) COMMENT 'email::' AFTER nickname ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN password password varchar(255) COMMENT 'password::' AFTER email ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN date_joined date_joined date COMMENT '::' AFTER password ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN active active int DEFAULT '0' COMMENT '::' AFTER date_joined ;
          ALTER TABLE my-recipe-app-1fe491.users CHANGE COLUMN token token varchar(255) COMMENT '::' AFTER active ;
          ","m_currentThread":0}

          1 Reply Last reply Reply Quote 0
          • Wassim
            Wassim last edited by

            @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.

            1 Reply Last reply Reply Quote 1
            • M
              malky shlomowith last edited by

              thanks for your help!

              1 Reply Last reply Reply Quote 0
              • Wassim
                Wassim last edited by

                you're welcome 🙏 !

                1 Reply Last reply Reply Quote 0
                • M
                  malky shlomowith last edited by

                  @Wassim said in Regster API Function.:

                  how many rows have been affected in the return of sqlExecuteRawQuery

                  how do I check it?

                  1 Reply Last reply Reply Quote 0
                  • Wassim
                    Wassim last edited by

                    https://www.npmjs.com/package/appdrag-cloudbackend

                    You can JSON.parse(response); and check numberOfAffectedRows.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post