I am getting an error that "directory is not defined" I really don't know if it means in my code or my json file. Here is the json file which I checked the format with https://jsonformatter.curiousconcept.com/
我收到一个错误,“目录未定义”我真的不知道它是否在我的代码或我的json文件中。这是json文件,我用https://jsonformatter.curiousconcept.com/检查了格式
[
{
"directory": "C:\\Users\\PCAdmin\\Downloads\\jsonin",
"dirID": "dir01"
},
{
"directory": "C:\\Users\\PCAdmin\\Downloads\\jsonout",
"dirID": "dir02"
}
]
Here is my code which based on examples I've seen should work yet I can't seem to get past the error;
这是我的代码基于我见过的示例应该工作但我似乎无法克服错误;
'use strict';
var fs = require('fs');
var jsonObj = require("./jsonDirectories.json");
for(directory in jsonObj) {
console.log("Dir: "+jsonObj.directory);
}
I'm sure it's something stupid but any direction would be appreciated
我敢肯定这是愚蠢的,但任何方向都会受到赞赏
3 个解决方案
#1
1
The error means that the variable directory
on line 4 of your code was not initialized. The following code will fix that bug:
该错误意味着代码的第4行上的变量目录未初始化。以下代码将修复该错误:
'use strict';
var fs = require('fs');
var jsonObj = require("./jsonDirectories.json");
for (var dirInfo in jsonObj) {
console.log("Dir: " + dirInfo.directory);
}
However, this still does not do what you want, because the in
operator does not work this way for arrays. The in
operator is generally used to get the keys of objects (and then should still be used carefully).
但是,这仍然无法执行您想要的操作,因为in运算符对数组不起作用。 in运算符通常用于获取对象的键(然后仍应小心使用)。
To loop over your array of directory info, what you want is the following:
要遍历您的目录信息数组,您需要的是以下内容:
'use strict';
var fs = require('fs');
var jsonObj = require('./jsonDirectories.json');
jsonObj.forEach(function(dirInfo) {
console.log('Dir: '+dirInfo.directory);
}
(I also removed the mixed single and double quotes, which is good practice.)
(我也删除了混合单引号和双引号,这是很好的做法。)
#2
1
You need to declare the directory
variable before using it:
您需要在使用之前声明目录变量:
for (let item of jsonObj) {
// This line also needed fixing:
console.log("Dir: ", item.directory);
}
#3
0
when you use for(directory in jsonObj)
in node.js, directory will be assigned to the index of each of the items, not the value. so you can use jsonObj[directory]
to get the directory.
当你在node.js中使用for(目录在jsonObj中)时,目录将被分配给每个项目的索引,而不是值。所以你可以使用jsonObj [directory]来获取目录。
But in my mind, this alternative is better:
但在我看来,这种替代方案更好:
jsonObj.forEach( function(directory) {
console.log("Dir: "+ directory);
});
#1
1
The error means that the variable directory
on line 4 of your code was not initialized. The following code will fix that bug:
该错误意味着代码的第4行上的变量目录未初始化。以下代码将修复该错误:
'use strict';
var fs = require('fs');
var jsonObj = require("./jsonDirectories.json");
for (var dirInfo in jsonObj) {
console.log("Dir: " + dirInfo.directory);
}
However, this still does not do what you want, because the in
operator does not work this way for arrays. The in
operator is generally used to get the keys of objects (and then should still be used carefully).
但是,这仍然无法执行您想要的操作,因为in运算符对数组不起作用。 in运算符通常用于获取对象的键(然后仍应小心使用)。
To loop over your array of directory info, what you want is the following:
要遍历您的目录信息数组,您需要的是以下内容:
'use strict';
var fs = require('fs');
var jsonObj = require('./jsonDirectories.json');
jsonObj.forEach(function(dirInfo) {
console.log('Dir: '+dirInfo.directory);
}
(I also removed the mixed single and double quotes, which is good practice.)
(我也删除了混合单引号和双引号,这是很好的做法。)
#2
1
You need to declare the directory
variable before using it:
您需要在使用之前声明目录变量:
for (let item of jsonObj) {
// This line also needed fixing:
console.log("Dir: ", item.directory);
}
#3
0
when you use for(directory in jsonObj)
in node.js, directory will be assigned to the index of each of the items, not the value. so you can use jsonObj[directory]
to get the directory.
当你在node.js中使用for(目录在jsonObj中)时,目录将被分配给每个项目的索引,而不是值。所以你可以使用jsonObj [directory]来获取目录。
But in my mind, this alternative is better:
但在我看来,这种替代方案更好:
jsonObj.forEach( function(directory) {
console.log("Dir: "+ directory);
});