【发布时间】:2016-01-10 22:50:19
【问题描述】:
我收到“TypeError:无法读取未定义的属性‘长度’?”错误,当我尝试从保存在文件中的联系人中获取特定联系人(提供电话号码)时。
exports.query = function(number) {
var json_result = JSON.parse(read_json_file());
console.log(json_result);
var result = json_result.result;
for (var i = 0; i < result.length; i++) {
var contact = result[i];
if (contact.primarycontactnumber === number) {
return contact;
}
}
return null;
};
我将上面的代码称为:
app.get('/contacts/:number', function(request, response) {
response.setHeader('content-type', 'application/json');
response.end(JSON.stringify(contacts.query(request.params.number)));
});
我的 read_json_file 方法的代码是:
function read_json_file() {
var file = './data/contacts.json';
return fs.readFileSync(file);
}
【问题讨论】:
-
json_result登录到控制台时的内容是什么?