簡體   English   中英

在運行Type腳本代碼時出現未定義的錯誤

[英]While running Type Script code getting undefined error

這是類型腳本中非常簡單的程序,用於從yaml讀取值。

運行時我變得不確定

const yaml = require('js-yaml');
const fs = require('fs');
const cred = yaml.safeLoad(fs.readFileSync('users.yml', 'utf8'));

function getUser(name:string):string { 
  return cred.name; 
} 
let v='admin'
let msg = getUser(v)  
   console.log(msg)

讓我知道為什么我們變得不確定

您想要訪問名稱為變量的屬性,因此您應該執行以下操作:

function getUser(name:string):string { 
  return cred[name];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM