简体   繁体   中英

node.js program using mssql module - connecting to a SQL Server database fails

I've got this very simple node.js program. The program adds a record to an SQL Server database. I'm trying to code it as a synchronous operation.

It returns an error:

"config.server" property is required and must be of type string.

However the property exists and is a string.

Does anyone know why this is the case?

var AWS = require('aws-sdk');
const sql_server = require('mssql');

exports.handler = (event, context, callback) => {
    console.log('Received event:', event);
    var DB_status;
    DB_status=Add_to_DB(event);
    if (DB_status=="OK") {
      var response = {
        "isBase64Encoded": false,
        "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
        "statusCode": 200,
        "body": "{\"result\": \"Success.\"}"
      };
    }
    else {
      var response = {
        "isBase64Encoded": false,
        "headers": { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' },
        "statusCode": 200,
        "body": "{\"result\": \"Database Error - " + DB_status + ".\"}"
      };
    };
    callback(null,response);
}

async function Add_to_DB (event) {
    let DB_status="OK";
    const config = {
      user: 'sa',
      password: 'sapassword',
      database: 'serverless-example',
      server: 'serverless-example.cilqefqosmtc.ap-southeast-2.rds.amazonaws.com'
    };

    var sql = 'INSERT INTO UserTbl(Name,Email,Message)';
    sql = sql + ' VALUES(\"' + event.name + '\",\"' + event.email + '\",\"' + event.message + '\")';

    try {
      let dbConn = await sql_server.connect();
      let request = new sql_server.Request(dbConn);
      await request.query(sql);
    }
    catch (err) {
      // Error running our SQL Query
      console.error("ERROR: Exception thrown running SQL", err);
      DB_status=err;
    }
    return DB_status;
}

Sorry to all. There is a typo there which I did not spot - I did not end up using the conif variable.

THanks to all for looking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM