여유상점_0919 : 9) Insert and Update Notices( 공지사항 )

4391 단어 mysqlnodejsmysql

PB.1 : Make unique key and go through loop until we find that there is no existing data that matches the unique key

I used the ( do while ) + ( continue )
but following error occurs

C:\Users\user.DESKTOP-3NN2QR0\Desktop\갭이어\GapYearCode\markets-master\server\router\admin\Admin_Notice.js:37er\router\admin\Admin_Notice.js:37
                    continue
                    ^^^^^^^^

SyntaxError: Illegal continue statement: no surrounding iteration statement
    do{
        UniqueKey = shortid.generate()

        connection.db_rest.query(`SELECT DISTINCT from notice WHERE Key = "${UniqueKey}"`, 
        (err, result) => {

            if(err){
                console.log(err)
                return res.status(404).json({ message : "failed"})
            }else{

                // 우리가 새로 만든 unique한 key값에 해대 만약 여전히 값이 존재한다면... 새롭게 unique값 만드는 process로 다시 들어간다
                if(result[0]){
                    console.log("Data already exist that matches the unique key")
                    continue

                }else{
                    // 만약 만든 unique한 key값에 대해 값이 존재하지 않는다면 새롭게 넣으면 된다
                    connection.db_rest.query('INSERT INTO notice SET ?' , {
                        Key : UniqueKey,
                        Title : Title,
                        Writer : Writer,
                        Id : Id,
                        WriteDate : new Date(Time),
                        Category: Program,
                        Content : Content
                    }, (error, result) => {
                        if(error){
                            console.log("Error occurred")
                            console.log(error)
                            return res.status(404).json({ message : "failed"})
                        }
                        NoUnique = true;
                        return res.status(404).json({ message : 'success'})
                    }) // insert query 
                } // no existing data that matches unique key
            } // no error
        }) // select query

    } while(NoUnique != true) // while : until we find unique key value
    

Trial 1: Do not Include "insert query" inside the loop

If we find out there is no data that matches the unique key, than break the loop . and then insert

PB.2 MySQL Query Error

connection.db_rest.query("SELECT DISTINCT * from notice WHERE Key = ?", [UniqueKey], 
        (err, result) => {
            if(err){
                console.log(err)
                return res.status(404).json({ message : "failed"})
            }else{

                console.log("query ")
                // 우리가 새로 만든 unique한 key값에 해대 만약 여전히 값이 존재한다면... 새롭게 unique값 만드는 process로 다시 들어간다
                if(result[0]){
                    console.log("Data already exist that matches the unique key")
                }else{
                    console.log("no problem")
                    NoUnique = true;
                }
            } // no error
        }) // select query

ERROR

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 
near 'Key = '5218BEgMg'' at line 1

Since Selecting infos by other column ( etc. Title, Writer ) works, "Key" column seemes to be related with problem

So I Checked the "Key" Column

I tried to drop the "Key" Column but follwing Error Occurs

mysql> alter table notice drop Key;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Solution : use 'backticks '

mysql> alter table notice drop column Key;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

Why?!?!
Maybe I included "white spaces" in "Key" Column
while creating that column

End

I tried to apply loop but it is not neeeded
since it is less likely that shortid library will generate same value

좋은 웹페이지 즐겨찾기