Node js中的GET路由的参数传递错误

时间:2018-10-28 05:37:19

标签: node.js mongodb

当我尝试转到/newmerman时遇到此错误。

  

错误:传入的参数必须是12个字节的单个字符串或24个十六进制字符的字符串       在新的ObjectID(/Users/ediewoelfle/Developer/Mermanager/node_modules/bson/lib/bson/objectid.js:57:11)       在app.get(/Users/ediewoelfle/Developer/Mermanager/app/routes/mermen_routes.js:25:27)       在Layer.handle [作为handle_request](/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/layer.js:95:5)       在下一个(/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/route.js:137:13)       在Route.dispatch(/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/route.js:112:3)       在Layer.handle [作为handle_request](/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/layer.js:95:5)       在/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/index.js:281:22       在param(/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/index.js:354:14)       在param(/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/index.js:365:14)       在Function.process_params(/Users/ediewoelfle/Developer/Mermanager/node_modules/express/lib/router/index.js:410:3)

这是我的 mermen_routes.js

// Create a merman
app.get('/newmerman', (req, res) =>
{
    res.render('newmerman')
});
app.post('/addmerman', (req, res) =>
{
  const merman = { name: req.body.name, location: req.body.location };
  db.collection('mermen').insert(merman, function(err, doc)
  {
    if (err)
    {
      res.send("There was a problem adding the merman to the kingdom.");
    }
    else
    {
      res.redirect('mermenlist')
    }
  });
});

还有我的newmerman的html表单。

<h1>Do you want to build a merman?</h1>
<form action="/addmerman" method="POST">
  <input type="text" placeholder="Ken" name="name" />
  <input type="text" placeholder="castle" name="location" />
  <button type="submit" value="Yes I want to build a merman."></button>
</form>

0 个答案:

没有答案