当前位置: 首页 > 面试题库 >

JSON + Node.js-意外令牌o

卓胜
2023-03-14
问题内容

我刚刚在大学课程中开始使用node.js和json对象。本周我们的任务之一是创建一些json对象,并将对象的一部分提取到html页面中。我以为自己对此有很好的掌握,但是尝试启动节点时遇到了错误。如果我删除了colleges对象和parse语句,则节点运行正常。

这是我运行“ node index.js”时遇到的错误:

undefined:1
[object Object],[object Object],[object Object],[object Object],[object Object
 ^
SyntaxError: Unexpected token o
at Object.parse (native)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/requestHandlers.js:13:20)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/ubuntu/node_stuff/node_json/index.js:3:23)
at Module._compile (module.js:449:26)

这是我正在使用的代码。

var querystring = require("querystring"),
fs = require("fs"),
formidable = require("formidable");

var colleges = [
{"name":"A-B Tech","street":"340 Victoria Road","city":"Asheville","state":"NC","zip":"28801","phone":"828-254-1921"},
{"name":"UNC Asheville","street":"1 University Heights","city":"Asheville","state":"NC","zip":"28804","phone":"828-251-6600"},
{"name":"UNC Charlotte","street":"9201 University City Blvd","city":"Charlotte","state":"NC","zip":"28223","phone":"704-687-8622"},
{"name":"Western Carolina","street":"North Carolina 107","city":"Cullowhee","state":"NC","zip":"28723","phone":"877-928-4968"},
{"name":"NC State","street":"2200 Hillsborough","city":"Raleigh","state":"NC","zip":"27695","phone":"919-515-2011"}
];

var college = JSON.parse(colleges);

function abtech(response) {
console.log("Request handler 'abtech' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li>' + college[0].name + '</li>'+
'<li>' + college[0].street + '</li>'+
'<li>' + college[0].city + ' ' + college[0].state + ' ' + college[0].zip + '</li>'+
'<li>' + college[0].phone + '</li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

function unca(response) {
console.log("Request handler 'abtech' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<ul>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'<li></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

function home(response) {
console.log("Request handler 'home' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Welcome to College</h2>'+
'<p>Where would you like to visit?</p>'+
'<ul>'+
'<li><a href="/colleges">Colleges</a></li>'+
'<li><a href="/hours">Hours of Operation</a></li>'+ 
'<li><a href="/start">Upload a Photo</a></li>'+
'<li><a href="/show">View Gallery</a></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

function colleges(response) {
console.log("Request handler 'colleges' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Colleges</h2>'+
'<ul>'+
'<li><a href="/abtech">A-B Tech</a></li>'+
'<li><a href="/unca">UNC Asheville</a></li>'+
'<li><a href="/uncc">UNC Charlotte</a></li>'+
'<li><a href="/wcu">Western Carolina</a></li>'+
'<li><a href="/ncsu">NC State</a></li>'+
'</ul>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

function hours(response) {
console.log("Request handler 'gallery' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Hours of Operation</h2>'+
'<table>'+
'<tr><td>Monday - Thursday</td><td>9 a.m. - 7 p.m.</td></tr>'+
'<tr><td>Friday</td><td>9 a.m. - 5 p.m.</td></tr>'+
'<tr><td>Saturday</td><td>9 a.m. - 12 p.m.</td></tr>'+
'</table>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}


function start(response) {
console.log("Request handler 'start' was called.");

var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" '+
'content="text/html; charset=UTF-8" />'+
'</head>'+
'<body>'+
'<h1>Upload a file</h2>'+
'<p>It will be shown on the /show url after</p>'+
'<form action="/upload" enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="upload" multiple="multiple">'+
'<input type="submit" value="Upload file" />'+
'</form>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}

function upload(response, request) {
console.log("Request handler 'upload' was called.");

var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");

/* Possible error on Windows systems:
   tried to rename to an already existing file */
fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg", function(err) {
  if (err) {
    fs.unlink("/home/ubuntu/node_stuff/node_assignment/test.jpg")
    fs.rename(files.upload.path, "/home/ubuntu/node_stuff/node_assignment/test.jpg");
  }
});
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
}

function show(response) {
console.log("Request handler 'show' was called.");
fs.readFile("/home/ubuntu/node_stuff/node_assignment/test.jpg", "binary", function(error, file) {
if(error) {
  response.writeHead(500, {"Content-Type": "text/plain"});
  response.write(error + "\n");
  response.end();
} else {
  response.writeHead(200, {"Content-Type": "image/jpg"});
  response.write(file, "binary");
  response.end();
}
});
}

exports.start = start;
exports.upload = upload;
exports.show = show;
exports.home = home;
exports.colleges = colleges;
exports.hours = hours;
exports.abtech = abtech;

任何有关我做错事情的提示将不胜感激。我的教练很难在周末得到帮助,所以我真的没有其他可转向的地方。谢谢。


问题答案:

您的colleges变量已经是有效的JavaScript对象。您不必JSON.parse在其上使用。

JSON.parse期望String第一个参数,但您提供Object。因此,它被强制显示为String类似于您在错误消息中看到的外观。

对于其余的代码,您可能想看一下Express或Zappa,以便能够将代码编写得更紧凑;)



 类似资料:
  • 问题内容: 尝试进行呼叫并检索一个非常简单的一行JSON文件。 这是RAW请求: 这是RAW回应: 响应中返回了JSON(红色:#f00),但Chrome报告了 Uncaught SyntaxError:Unexpected token:colors.json:1 如果我直接导​​航到url本身,则返回JSON并显示在浏览器中。 如果我将colors.json的内容粘贴到JSLINT中,则json

  • 问题内容: 我不明白怎么了。节点v5.6.0 NPM v3.10.6 代码: 错误: 问题答案: 更新3: 从Node 13开始 ,您可以使用.mjs扩展名,也可以在package.json中设置“ type”:“ module”。你 并不 需要使用标志。 更新2: 从Node 12开始 ,您可以使用扩展名,也可以在package.json中进行设置。并且您需要运行带有标志的节点。 更新: 在节点

  • 我不明白怎么了。节点V5.6.0 NPM V3.10.6 代码: 错误:

  • My HQL: 我收到以下错误: 我是HQL的新手。我想通过代码动态设置间隔值。我设置了等于比较的参数值。我读过http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/post,但我不知道在没有其他符号的情况下如何设置timeOffSet值。 斯纳克斯。 编辑: 我试过?及其工作良好,但我想用来实现这一点。

  • 问题内容: 我不明白怎么了。节点v5.6.0 NPM v3.10.6 编码: 错误: 问题答案: 更新3:从Node 13开始,你可以使用扩展名,也可以在中设置。你并不需要使用–标志。 更新2:从Node 12开始,你可以使用扩展名,也可以在中进行设置。并且你需要运行带有–experimental-modules标志的节点。 更新:在节点9中,它在标志后面启用,并使用扩展名。 虽然确实是ES6的一

  • 问题内容: 为什么每次我做时:- 它只是解析很好,但是当我这样做时:- 它给我一个错误,说:- 问题答案: 您要它解析JSON文本(不是)。那是无效的JSON,字符串必须用双引号引起来。 如果要与第一个示例等效: