显示单一路线和删除路线

时间:2018-04-20 03:11:45

标签: node.js express routes

nodejs,js和express的新手。我在显示单个嵌套文档时遇到困难,因为查询上有多个req.params.id。它工作但我收到一个奇怪的错误。还需要查询帮助从父项中删除子文档并删除它。错误和代码如下所示。

路线

//Show Single Route
router.get("/employee/:id/qcerror/:qcerror_id", isLoggedin, function(req, res){
    Employee.findById(req.params.id, function(err, employee) {
        if(err){
            console.log(err);
        } else {
    QcError.findById(req.params.qcerror_id, function(err, foundQcError){
        if(err){
            console.log(err);
            res.redirect('back');
        } else {
    res.render('qcerrors/show', {employee: req.params.id, qcerror: foundQcError});
        }
    });
    }
    });
});

表格

<a href="/employee/<%= employee._id %>/qcerror/<%=qcerror._id%>">

错误

 message: 'Cast to ObjectId failed for value "app.js" at path "_id" for model "QcError"',
  name: 'CastError',
  stringValue: '"app.js"',
  kind: 'ObjectId',
  value: 'app.js',
  path: '_id',
  reason: undefined,
  model: 
   { [Function: model]
     hooks: Kareem { _pres: [Object], _posts: [Object] },
     base: 
      Mongoose {
        connections: [Object],
        models: [Object],
        modelSchemas: [Object],
        options: [Object],
        _pluralize: [Function: pluralize],
        plugins: [Object] },
     modelName: 'QcError',
     model: [Function: model],
     db: 
      NativeConnection {
        base: [Object],
        collections: [Object],
        models: [Object],

1 个答案:

答案 0 :(得分:0)

您正在尝试获取无效ID。

如果要检查24个十六进制字符的字符串,可以使用正则表达式。

const checkID = new RegExp("^[0-9a-fA-F]{24}$");

const mongodb = require("mongodb"),
objectid = mongodb.BSONPure.ObjectID;

console.log(objectid.isValid('yourObjectID'));