1

I have created a SQL server with a table that has data in on a server I have local access to. I have also created an "API" on that same server to get the information from the SQL server so it can be read by my angular application.

At the moment, I can read the rows in the SQL server and have them displayed in my angular application, but, I want to be able to update the SQL table from my angular app (through the API).

This is my API (sqlserver.js):

var express = require('express');
var app = express();
var cors = require('cors');

app.use(cors())

app.get('/', function (req, res) {

    var sql = require("msnodesqlv8");

    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName";
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});


var server = app.listen(3097, function () {
    console.log('Server is running..');
});

I want to be able to use the query

const query = "SELECT * from tableName WHERE ID LIKE <inputFromAngular>"

But I am not sure how to get the parameter from angular into the sqlserver.js. (If I can do this then it will lead to updating the SQL Table using SET

In my angular app this is how I am calling the sqlserver.js to display the SQL table:

import { HttpClient } from '@angular/common/http';

constructor(
    private httpService: HttpClient
    ) { }


this.httpService.get("http://servername:3097").subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

I have tried using this.httpService.post() but I wasn't sure how to get the parameters in the API?

1 Answer 1

1

You will need to change you get method to send params from ui side and get params on backend side so add a server side method like

app.get('/:id', function (req, res) {

    var sql = require("msnodesqlv8");
 var id = req.params.id,
    // config for your database
    var config = "server=servername;Database=dataBaseName;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}"
    const query = "SELECT * FROM tableName WHERE ID LIKE "+id;
    // connect to your database

    sql.query(config, query, (err, rows) => {
        res.send(rows)
     });
});

then on angular side

this.httpService.get("http://servername:3097/"+yourid).subscribe(response => {
        console.log(response)
        this.sqlData = response;
      })

Also I would suggest you to create a root like /yourapiname/:id instead of /:id and url on angular side should be "http://servername:3097/yourapiname"+yourid because your current root can result to confliction.

Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant that was exactly what I was after. It works if I am sending a single parameter but when I try to send a JSON object it runs [object Object] on the server. Can i only send strings with this method?
for that you will have to create a post request.you are trying to create some record right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.