【问题标题】:Using a string interpolation in a string template undefined object attribute value在字符串模板未定义对象属性值中使用字符串插值
【发布时间】:2022-01-03 07:55:37
【问题描述】:

我遇到了这个奇怪的错误:

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]
  };

输出

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'

为什么它在 cosole.log 中定义为 1,但在第三个 console.log 中却没有定义。
我的想法是因为一些异步的东西,但它在同一个文件中。我的第二个想法是它正在取得所有权,但不是一种低级语言。所以我的新假设是字符串插值有一个内部错误。

还有其他情况会是这样的其他错误吗?

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

nodejs 版本:v16.13.0

【问题讨论】:

  • 请显示构造 testObject 的代码,atrr 的值是 1。仅仅因为某些内容在同一个文件中并不意味着异步行为以某种方式被否定。
  • 另外,请发minimal reproducible example,因为这个简单的代码无法重现您的结果。
  • 如果交换第二条和第三条日志行(即先调用模板第一个),您会在1 之前得到undefined,还是输出不变?如果输出顺序没有改变(即1 仍然在前),那么它实际上可能是一些异步的东西,与模板无关。
  • @HereticMonkey 好的。更新了
  • @RandyCasburn Srr 我不能分享我的代码。并在没有它的情况下重现错误,我想我不知道该怎么做,因为这是一个非常奇怪的错误。

标签: javascript node.js typescript


【解决方案1】:

将这个放在答案中只是因为我需要传达多行代码,而这些代码在评论中将无法辨认。

当我将它放入一个文件并使用 nodejs 运行该文件时:

const testObject = {
    atrr: 1
};

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

我在控制台中得到这个输出:

{ atrr: 1 }
1
1

这正是我们所期望的。因此,显然您的对象或环境周围有些不同。我期望undefined 的唯一方法是attrr 属性的值是否没有.toString() 方法,或者该方法返回undefined

请记住,模板将检查您传递给它的值的类型,如果它不是字符串,它会尝试将其转换为字符串。显然,字符串转换导致undefined

如需进一步帮助,请准确说明 testObject 是如何声明的,并编辑您的问题以显示一个最小的、可重现的示例。

【讨论】:

  • 哦,我不知道字符串模板会搜索对象的 .toString 方法。所以...如果我的对象没有 .toString 这将是非常逻辑错误,但我有其他工作正常的对象
猜你喜欢
  • 2021-03-18
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2010-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多