Error deleting a BD widget from a column

Asked

Viewed 93 times

-1

I’m trying to create a Postings in Node. A post can be created, shown or deleted. For this I created a table called posts with the following fields :

table.string('category').notNullable()
table.string('title').notNullable()
table.string('description').notNullable()

This given my system will have 3 possible operations for this Posting entity : Create, Index (show) and Delete. To carry out the deletion of a post I wish to inform the title of one already present in the BD and then this be excluded. Here is the implementation of the operation of Destroy in my Controller :

async delete (request, response){

        const{title} = request.params

        const postagem = await connection('posts')
            .where('title', title)//significa procurar um título que seja igual ao const{title} = request.params definido acima
            .select('title')//selecionando apenas a coluna title da tabela
            .first()//como terá apenas um resultado, o first deletará o primeiro resultado do título que vier

            if(postagem.title != title){//se o título da postagem que colocamos for diferente da do título dentro do bd ...
                return response.status(401).json({error : 'Operation not permitted'})//não autorizado, ou seja, erro
            }

            await connection('posts').where('title', tile).delete()
    }

Once done, I make a request like DELETE in JSON format as follows :

{
    "title": "<titulo da Postagem a ser excluida>"
}

And I get the following error message :

(Node:7699) Unhandledpromiserejectionwarning: Error: Undefined Binding(s) Detected when compiling FIRST. Undefined column(s): [title] query: select title_id from posts Where title = ? limit ?

How can I delete a Post by sending the title field in JSON format as demonstrated?

1 answer

1

For any change and/or deletion of data in a database, it is necessary to have the line ID you refer to.
Only the title is not accepted because theoretically there can be other titles (read strings) equal, already with an ID this cannot and should not occur.
The JSON object needs to look like this:

{
   "title_id": 1,
   "title": "texto"
}
  • Thanks for the reply. Based on it I refactored my code a little, and added a new field to my table. It is now as follows :

  • //primary key that auto-increments every post table.increments('post_id'). Primary table.string('Category'). notNullable() table.string('title'). notNullable() table.string('Description'). notNullable()

  • Instead of wanting to delete by 'title' I am now passing the 'post_id'. The logic in the controller follows the same with the difference that it is now post_id (that is, everything above as title agr is post_id). Passing the post_id in JSON format it returns no error, only informs the following message : Cannot DELETE /posts . Can you imagine what might have caused this?

  • Usually this message appears when the route in question has not implemented this HTTP protocol

  • Mine is currently : Routes.delete('/posts/:post_id', Postcontroller.delete)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.