Error POST http:abc/ 404 (Not Found) in angular2+ and nodejs

99封情书 提交于 2019-12-23 02:44:10

问题


I am making a http request from my angular2+ code to database present in node.js file. The ajax call from angular2+ hits the controller.js file and then redirects to service.js file which has the connection to the database:

angular.ts ==> controller.js ==> service.js

From the database the service.js file gives the output to controller.js file and then answers the ajax call to angular.ts file:

service.js ==> controller.js ==> angular.ts

However, I am getting the error:

POST http://localhost:8096/dashboard/abcEntireSuccess1/ 404 (Not Found)

UPDATED Cannot GET /dashboard/experianEntireSuccess1/

And one more issue -

   UnauthorizedError: No authorization token was found

And one more issue -

After coming back from the hit in service.js which has the data i want ==> to => controller.js , here the data is acquired is undefined. As seen below -

The output on Nodejs -

output -

service.js

  closed connection
  yessss [ RowDataPacket { ....

controller.js

  we are coming back to controller undefined

  some error occured of abcEntireSuccess1

My Entire Code: UPDATED

abc.component.ts

viewAbcEntireSuccess1() {


var url = config.url;
var port = config.port;


this.http.post("http://" + url + ":" + port + "/dashboard  
  /abcEntireSuccess1/", this.emptyObj
  , { headers: new Headers({ 'Authorization': 'Bearer ' + 
    localStorage.getItem('Token') }) })
  .map(resultConnection => this.resultConnection = 
    resultConnection.json(), )
   .subscribe((res: Response) => {

     this.records = res;

     this.resultConnectionlength = this.resultConnection.length;

   });

 }

abc.controller.js

  router.post('/experianEntireSuccess1',experianEntireSuccess1);
  module.exports = router;


    function abcEntireSuccess1(req, res) {


    dashboardService.abcEntireSuccess1(req.body)
      .then(function (result) {
        console.log("we are coming back to controller",result)
        if (result.length > 0) {
            console.log("we get data in node of abcEntireSuccess1 :: 
         " + Object.values(result));
            console.log(result.length + " record found  ");
            res.send(result);
           } 
         else {
            result=[];
            res.send(result);
         }
     })
     .catch(function (err) {
        res.status(400).send(err);
        console.log("some error occured of abcEntireSuccess1");
      });
      }

abc.service.js

  async function abcEntireSuccess1() {
  console.log("function called")
   const db = new Database();
   await db.query(`select * from TRANSACTION_PAYLOAD where INTERFACE_NAME
    = 'Abc' AND (STATUS ='SUCCESS_RESPONSE')`
    ).then(rows => {

    console.log("closed connection");

    console.log("yessss",rows)
    return rows;

     });
    };

   class Database {
   constructor() {

      this.connection = mysql.createConnection({
        host: "127.0.0.1",
        user: "abc",
        password: "abc",
        database: "DB"
      });

    }


  query(sql, args) {
    console.log("sql is", sql)
    return new Promise((resolve, reject) => {
        this.connection.query(sql, (err, rows) => {
            console.log("connection function called")
            if (err) {
                console.log("error is", err)
                return reject(err);
            }

            console.log("rows are",rows);
            resolve(rows);
        });
       });
     }
   close() {

     console.log("calling connection close")
     return new Promise((resolve, reject) => {
        console.log("called connection close")
        this.connection.end(err => {
            if (err){
                return reject(err);
            }

            resolve();
        });
     });
     }
     }

来源:https://stackoverflow.com/questions/51111931/error-post-httpabc-404-not-found-in-angular2-and-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!