Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Postman error or code syntax

anuraa

Legendary Coder
Hi:
I am trying to get an understanding of server side programming. I am trying to test my POST Api with POSTMAN.

1. created server.js
2. created database.js
3. ran "npm init -y"
4. ran "npm install express"
5. ran "npm install sqlite3"
6. ran "node server.js"

I have included the coding of 1. and 2. I have attached two screen shots of VB Terminal and POSTMAN. POSTMAN gives an error. Thanks for your helps.

JavaScript:
var express = require("express");
var app = express();
var db = require("./database.js");

var bodyParser = require("body-parser");
const { request, response } = require("express");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

let HTTP_PORT = 8081;

app.listen(HTTP_PORT,()=>{
    console.log("Server is running on %PORT%".replace("%PORT%", HTTP_PORT))
});

app.post("/api/customer1/", (req, res, next) => {

    try {
        var errors = []

        if (!req.body) {
            errors.push("An invalid input");
        }

        const { name,
            address,
            email,
            dateOfBirth,
            gender,
            age,
            cardHolderName,
            cardNumber,
            expiryDate,
            cvv,
            timeStamp
        } = req.body;

        var sql = 'INSERT INTO customer (name, address, email, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv, timeStamp) VALUES (?,?,?,?,?,?,?,?,?,?, ?)'
        var params = [name, address, email, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv, timeStamp]
        db.run(sql, params, function (err, result) {

            if (err) {
                res.status(400).json({ "error": err.message })
                return;
            } else {
                res.json({
                    "message": "success",
                    "data": req.body,
                    "id": this.lastID
                })
            }

        });
    } catch (E) {
        res.status(400).send(E);
    }
});


JavaScript:
const { text } = require('body-parser');
const { getEventListeners } = require('stream');

var sqlite3 = require('sqlite3').verbose();
const DBSOURCE = 'db.sqlite';

let db = new sqlite3.Database(DBSOURCE,(err) => {
    if(err){
        console.error(err.message)
        throw err
    }else{
        console.log(`Connected to the SQlite Data base')
        db.run( 'CREATE TABLE customer (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name text,
        address text,
        email text,
        dateOfBirth text,
        gender text,
        age INTEGER,
        cardHolderName text,
        cardNumber INTEGER,
        expiryDate text,
        cvv INTEGER,
        timeStamp text
        )`, (err) => {
        if (err){
            // table already created
        }   else {
            var insert = 'INSERT INTO customer' (name, address, email, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv, timeStamp)
                db.run(insert,["anura ariyaratne", "no 3, Panadura", "[email protected]", "1990.02.04", "male", 55, "Anura Ariyartne", 12456789056, 12/2022, 562, "2022.12.31.23.59.59"])
        }
        } 

        )   
    }
  
});
module.exports = db;
 

Attachments

  • postman.png
    postman.png
    236.5 KB · Views: 2
  • VBscreen.png
    VBscreen.png
    136.5 KB · Views: 2
And is is difficult for me to help you if you don't give proper information. So what exactly is the problem now ? That you get the error "table customer already exists" ? If so, why does that surprise you ?
You always start with creating the table, and in the error handler of this statement you do the insert. This seems to me the wrong way around. The insert is your main purpose, start off with doing that. Should the insert fail with "table does not exist" (hopefully first time only) , create the table and retry the insert. The second time you get there, the insert just works because the table is there.
 
I want to add two validation rules to Cardnumber and Email. I just guessed and try to expand like email text “type” = Email and cardNumber INTEGER LENGTH = 12

I want to check with either documentation or with someone like you about my this expansion.
Thanks.
 
Time and again, it seems that most of what I write is flying right past you. This is getting really tiresome.
As for implementing constraints, see if this page helps you. I don't think there is a function to check an email address.
Personally I would implement such validations in the client, before even submitting to postman.
 

New Threads

Buy us a coffee!

Back
Top Bottom