Skip to content

lambda node.js using mssql module - connect error

0

Hi,
I've got this very simple node.js program to add a record to an RDS SQL Server database.
It returns an error:
"config.server" property is required and must be of type string.
However the property exists and is a string.
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;
}

Edited by: phillip-from-oz on Jun 2, 2019 7:06 PM

Edited by: phillip-from-oz on Jun 2, 2019 7:06 PM

asked 7 years ago2.5K views

1 Answer
0

Sorry there was typo in that on the connect method I did not give it the 'config' parameter.
Thanks.

answered 7 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.