WorrenGilgamesh
WorrenGilgamesh

Reputation: 59

Using a string interpolation in a string template undefined object attribute value

I'm having this weird error:

testObject.ts

import { OobjectA } from './OobjectA';
import { OobjectB } from './OobjectB';
import { OobjectC } from './OobjectC';

export class TestObject{
    private _atrrA: number=0;
    private _atrr1: string="";
    private _atrr2: number = 0;
    private _atrr3: number = 0;
    private _atrrBt: Date = new Date();
    private _atrrC: number =0;
    private _atrrD: Date = new Date();
    private _atrrError: number = 0;
    private _atrr4: number = 0;
    private _atrr5: number = 0;

    constructor(
        oobjectA: OobjectA,
        oobjectB: OobjectB,
        oobjectC: OobjectC
    ) {
        if (oobjectA !== undefined && 
            oobjectB !== undefined && 
            oobjectC !== undefined ) {
                this._atrrError = oobjectA.atrrError;
                this._atrr4 = oobjectB.atrr4;
                this._atrr5 = oobjectC.atrr5;
        }
      }

    set atrrA(atrrA){
        this._atrrA =atrrA
    }
    set atrr1(atrr1){
        this._atrr1 =atrr1
    }
    set atrr2(atrr2){
        this._atrr2 = atrr2
    }
    set atrr3(atrr3){
        this._atrr3 =atrr3
    }
    set atrrBt(atrrBt){
        this._atrrBt = atrrBt
    }
    set atrrC(atrrC){
        this._atrrC = atrrC
    }
    set atrrD(atrrD){
        this._atrrD = atrrD
    }
    set atrrError(atrrError){
        this._atrrError = atrrError
    }
    set atrr4(_atrr4){
        this._atrr4 = _atrr4
    }
    set atrr5(atrr5){
        this._atrr5 = atrr5
    }

    get atrrA(){
        return this._atrrA
    }
    get atrr1(){
        return this._atrr1
    }
    get atrr2(){
        return this._atrr2
    }
    get atrr3(){
        return this._atrr3
    }
    get atrrBt(){
        return this._atrrBt
    }
    get atrrC(){
        return this._atrrC
    }
    get atrrD(){
        return this._atrrD
    }
    get atrrError(){
        return this._atrrError
    }
    get atrr4(){
        return this._atrr4
    }
    get atrr5(){
        return this._atrr5
    }
  }

Controller.ts

try{
// ...more code
const oobjectA : OobjectA  = new OobjectA ()
oobjectA.atrrError = otherObject[0].atrrError
oobjectB.atrr4 = otherObject2[0].subObject[0].atrr4
const testObject: TestObject = new TestObject(oobjectA, oobjectB, oobjectC)
testObject.atrr1 = "string"
testObject.atrr2 = 1
testObject.atrr3 = 1;
const testObject_id: any = await create_user_function_ejecution_order[5].functionCU(testObject).catch(error => {
  create_user_function_ejecution_order[5].status=false
  create_user_function_ejecution_order[5].error = error
  console.log(error)
})
console.log("testObject",testObject.atrrA?.atrrA)
}catch(e){
// ... more code
}
// ... more code

file.ts

import {db} from "../../connection";

export const insert_data  = async(testObject) => {
    console.log(testObject)
    console.log(testObject._atrrError)
    console.log(testObject.atrrError)
    console.log(`${testObject.atrrError}`)
    console.log(`${testObject._atrrError}`)
    const queryString = `
            INSERT INTO table (column1, column2, column3, columnError, column4, column5)
            VALUES ('${testObject.atrr1}', ${testObject.atrr2}, ${testObject.atrr3}, ${testObject.atrrError} , ${testObject.atrr4}, ${testObject.atr5})
        `
    console.log(queryString)
    const result = await db.query(queryString)
    return result[0]
  };

output

testObject{
  _atrrA: 0,
  _atrr1: 'string',
  _atrr2: 1,
  _atrr3: 1,
  _atrrB: 2021-11-24T23:37:42.394Z,
  _atrrC: 0,
  _atrrD: 2021-11-24T23:37:42.394Z,
  _atrrError: 1,
  _atrr4: 1,
  _atrr5: 1
}
1
1
undefined
undefined
INSERT INTO table (INSERT INTO table (column1, column2, column3, columnError, column4, column5)
            VALUES ('string', 1, 1, undefined, 1, 1)

Error: Unknown column 'undefined' in 'field list'

Why it is defined with the value 1 in the cosole.log but in the 3rd console.log it is not defined.
My thought was because some asynchronous thing, but it is in the same file. My second thought was it is taking ownership, but is not a low level language. So my new hypothesis is that the string interpolation have an inside error.

Any other case were it can be other error like this?

devDependencies:
"ts-node": "^10.4.0",
"typescript": "^4.4.4"

nodejs version: v16.13.0

Upvotes: 0

Views: 670

Answers (1)

jfriend00
jfriend00

Reputation: 707446

Putting this in an answer just because I need to communicate multiple lines of code which won't be legible in a comment.

When I put this into a file and run that file with nodejs:

const testObject = {
    atrr: 1
};

console.log(testObject)
console.log(testObject.atrr)
console.log(`${testObject.atrr}`)

I get this output in the console:

{ atrr: 1 }
1
1

Which is exactly what we would expect. So, apparently there's something different around your object or your environment. The only way I would expect undefined is if the value of the attrr property was something that does not have a .toString() method or if that method returned undefined.

Keep in mind that the template is going to check the type of the value you passed it and, if it isn't a string, it's going to try to convert it to a string. Apparently that string conversion is resulting in undefined.

For any further help, please show exactly how testObject is declared and edit your question to show a minimal, reproducible example.

Upvotes: 2

Related Questions