ExpressJS SyntaxError:意外的标记C
我试图将我的string保存为一个逗号分隔数组,但是当我尝试使用JSON.parse
方法时,发送一个post方法并尝试保存logging时收到此错误:
SyntaxError: Unexpected token c at Object.parse (native) at router.route.post.get.res.render.blogpost (/Users/user/Desktop/Projects/node/blog/app/routes.js:106:34)
这是我的路线(错误来自blogpost.save):
router.route('/admin/posts/create') // START POST method .post(function(req, res) { console.log("New instance"); var blogpost = new Blogpost(); // create a new instance of a Blogpost model blogpost.title = req.body.title; // set the blog title blogpost.featureImage = req.body.featureImage; // set the blog image blogpost.blogUrl = blogpost.title.toLowerCase().replace(/\s+/g,"-"); blogpost.author = req.body.author; // set the author name blogpost.tagline = req.body.tagline; // set the tagline blogpost.content = req.body.content; // set the blog content blogpost.category = req.body.category; // set the category blogpost.tags = JSON.parse(req.body.tags.split(",")); // set the tags //Save Blog Post blogpost.save(function(err) { if (err) res.send(err); res.redirect(303, '/'); //NEEDS TO BE CHANGED }); }) // END POST method .get(isLoggedIn, function(req, res, blogpost) { res.render('pages/blogpost-create', { blogpost : blogpost }); });
JSON.parse
将一个string(有效的JSON)parsing成一个对象/数组。
你已经有了一个数组,就像你在做的一样
req.body.tags.split(",")
和split()
创build一个数组。 你不能parsing一个数组,但是你可以把它串联起来
- 需要Mongoose.js上的几列之一
- Mongoose / NodeJS – 来自数据库的string被检索,但被视为未定义
- ValidationException:ExpressionAttributeValues不能为空
- Express 3.0 HTTPS
- 从localStorage转换Backbone的Todo List示例
- 当执行一个更复杂的GET时,Express JS会返回404,然后localhost / users(localhost / users / about)
- 使用Express代理,如何更改相对的url?
- 无法将nodejs应用程序部署到heroku
- 在Angular / MEAN.io中路由安全漏洞?