Express:从req.params.id获取两个值,一个是我需要的,另一个是未定义的

时间:2018-02-08 00:03:00

标签: javascript node.js mongodb express mongoose

所以我使用Express和Pug,我能够将MongoDB中的文档ID成功传递到URL中。

以下是我用于Pug代码的内容,如果有帮助的话:

-var url = service.id
a(href="/service/" + url)

http://localhost:3000/service/5a7b340af018ca044a7b3a7b //the correct URL

我也可以使用标准的app.get函数获取id,并使用Mongoose将其传递给MongoDB,它确实会返回我想要的东西......首先。

app.get('/service/:sid', (req, res) => {
Services.findOne({ _id: req.params.sid }).exec(function(err, post) {
res.render('service.pug');

但似乎req.params.sid的值几乎立即被第二个值覆盖,这个值是未定义的。当我在console.log中时,我不断得到两个值,一个接着一个:

console.log(req.params.sid);
5a7b340af018ca044a7b3a7b // the value I want
undefined // the value that ends up overriding the id I need.

我已经做了我能想到的一切,以摆脱第二个,压倒一切的价值,但没有任何作用。有什么想法吗?

感谢。

0 个答案:

没有答案