SQL Server Try to fetch a data by code or using WHERE condition. But it returns nothing when getting the data

tvz2xvvm  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(89)

I'm running the project Angular using Node.js server MSSQL express. Can someone please check my code what is missing or mistake in getting the data using WHERE condition. Thank you.

/// Backend server API node.js MSSQL

checkCustPhone: (phone, callback) => {
    connection.query(
      `select * from tbl_customers where Phone_Number='${phone}'`,
      (error, result) => {
        if (error) {
          return callback(error);
        }
        return callback(null, result);
      }
    );
  }

    custData: (req, res) => {
    let phone = req.phone;
    checkCustPhone(req.params.phone,(error, result) => {
      if (error) {
        return res.send({ status: 0, data: result.recordset });
      }
      return res.send({ status: 1, data: result.recordset });
    });
  }

const express = require("express");
const router = express.Router();

  const {
  custData,
    } = require("./Tcontroller");
    router.get("/getcustomer", custData);

// Service.ts

getCustomerData(Phone_Number: any) {
    return this.http.get(`${this.url}/orders/getcustomer`,Phone_Number);
  }

/// Component.ts

Phone_Number = '9762985657';

    ngOnInit(): void {
       this.orderService.getCustomerData(this.Phone_Number).subscribe((res: any) => {
         console.log(res.data); }, 
        (error) => {
              this.alertService.onError(error.message);
          });
       }

I'm testing in console log in my Component to check if I get data using a Phone Number from the DB but it returns nothing.

am46iovg

am46iovg1#

From the response to your previous comment, you get a response when you run the phone number query against your backend db. Which is a great start.

Here are few errors I see on the angular side of your code.

  1. Incorrect request in your service.ts you're not passing the phone as a parameter in the request.the backend is expecting a phone as req.params. that is in your checkCustPhone(....). Without passing in the phone` as parameter through your service, your BE won't receive any phone as req.params to check your db against and return a results.

Modify your service.ts to include the phone as params.

eg.

getCustomerData(Phone_Number: any) {
    return this.http.get(`${this.url}/orders/getcustomer`, { params: { phone: Phone_Number } });
}

相关问题