gpt4 book ai didi

javascript - Nodejs - 使用属性读取器包时出错

转载 作者:行者123 更新时间:2023-12-03 06:16:45 25 4
gpt4 key购买 nike

我正在尝试使用 nodejsproperties-reader 包编写通用属性读取器实用程序。以下是实现:

var propertiesReader = require('properties-reader')
var path = require('path')

var property = getPropertyFile('./fileName.properties');
console.log(getProperty(property,'user1.username'))

function getPropertyFile(err, filePath){
if(err){
console.log("Error occured while reading properties file: "err);
}
else{
return propertiesReader(filePath);
}
}

function getProperty(err, propertyFile, propertyName){
if(err){
console.log("Error occured while getting properties: ",err);
}
else{
return propertyFile.get(propertyName)
}
}

在执行上述代码时,我在读取文件和获取属性值时遇到错误,如下所示:

E:\NodeJS\>node propertiesUtility.js
Error occured while reading properties file: ./locatorValues.properties
E:\NodeJS\propertiesUtility.js:24
return propertyFile.get(propertyName)
^

TypeError: propertyFile.get is not a function
at getProperty (E:\NodeJS\propertiesUtility.js:24:23)
at Object.<anonymous> (E:\NodeJS\propertiesUtility.js:5:13)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3

有人可以帮我纠正我的代码吗?

最佳答案

您正在将一个'user1.username'传递给getProperty(err, propertyFile, propertyName),它是一个纯字符串,现在propertyFile是一个纯字符串

'user1.username'.get(propertyName);

除非您已将方法 get() 添加到字符串原型(prototype),否则该方法不存在。

关于javascript - Nodejs - 使用属性读取器包时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com