Friday, 17 March 2017

Cannot read property 'save' of undefined in Express MySQL Module

I'm trying out the express mysql module and the node-mysql-wrapper and run into an issue with the below. When i run the code below. It gives me and error saying: Cannot read property 'save' of undefined.

var express = require('express');
import * as mysql from 'mysql';
import {Request, Response} fom 'express';
import * as mysql_wrapper from 'node-mysql-wrapper';

export class PeopleApi {

    private mysql_connection;

    constructor() {
        this.mysql_connection = mysql.createConnection({
            host: 'localhost',
            user: 'user',
            password: 'password',
            database: 'database'
        });
    }

    postPeople(req: Request, res: Response, next: Function) {
        let db = mysql_wrapper.wrap(this.mysql_connection);

        db.ready( () => {
            let data = {
                fname: req.body.fname,
                lname: req.body.lname,
                address: req.body.address
            };

            db.table('peopleData').save(data, (result) => { // error is from this line, the save method
                res.status(200.json(result);
            });

            db.destroy();
        });
    }
}

How do i fix this, what am i doing wrong.



via Ralph Marvin

No comments:

Post a Comment