-1
module.exports.addPerson = function (req) {
    return new Promise(function(resolve,reject){ 
        var name = req.body.name
         db.all('INSERT INTO person (name) VALUES ("'+name+")',function(err){
         if(!err){
            resolve("success") 
        }else{
            reject(err)
        }
})
})
    }

Because the ajax code does not return the error:


        var  name= $('#name').val();
        $.ajax({
          url: 'http://localhost:3000/class/add',
          data: {name:name},
          type: 'POST',
          success: function(data){
              alert('success')
          }, error: function(jqXHR, exception) {
              alert('error')
        }      
    })

In the event that the data is added correctly, a message will be displayed for that. In the case of a data conflict, no message will be displayed for that.

sam_prog
  • 13
  • 3
  • 1
    The first code snippet has a syntax error, is that the problem you’re referring to? Can you clarify specifically what problem you are observing and what debugging you have done? In your browser’s debugging tools, is the AJAX request being made? What is the server’s response? – David Aug 12 '23 at 14:15
  • The data is presented correctly, except in the case of data conflict, in the database it is assumed that the name is unique, and therefore when entering a previously existing name, one of the messages in Error is supposed to be displayed, and this does not happen – sam_prog Aug 12 '23 at 14:22
  • When you debug, what *does* happen? If you’re not familiar with your browser’s debugging tools, now is the time to start using those. You can observe the AJAX request and response on the network tab to see more information about the problem. – David Aug 12 '23 at 14:25
  • I was wrong in asking the question, I modified the code, I don't want to display the error in the console, I want to display it as a message box, but this is not done. – sam_prog Aug 12 '23 at 14:28
  • This is what is displayed in the console (node:6616) UnhandledPromiseRejectionWarning: Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: person.name – sam_prog Aug 12 '23 at 14:31
  • *Which* console displays that error? The server or the browser? What happens in the browser? What is the HTTP response code from the AJAX request? – David Aug 12 '23 at 14:33
  • The server displays it – sam_prog Aug 12 '23 at 14:36
  • Your favorite search engine can help you learn how to use your browser’s debugging tools. Please start there. – David Aug 12 '23 at 14:37
  • I would like to know why this is not turned on , error: function(jqXHR, exception) { alert('error') } – sam_prog Aug 12 '23 at 14:38
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – David Aug 12 '23 at 14:41
  • The browser displays XHR status = bending – sam_prog Aug 12 '23 at 14:57
  • Then it sounds like the server is never returning a response. Where is the code which handles the HTTP request on the server? How does that code handle a rejected Promise from the database interaction? – David Aug 12 '23 at 15:02

0 Answers0