【问题标题】:CastError: Cast to ObjectId failed for value "findByName" at path "_id" for model in mongooseCastError:对于猫鼬中模型的路径“_id”处的值“findByName”,转换为 ObjectId 失败
【发布时间】:2019-05-02 13:07:41
【问题描述】:

我遇到错误

CastError:路径中的值“findByName”转换为 ObjectId 失败 模型“产品”的“_id” 在新的 CastError (/Users/me/projects/nodejs/node_modules/mongoose/lib/error/cast.js:29:11) 在 ObjectId.cast (/Users/me/projects/nodejs/node_modules/mongoose/lib/schema/objectid.js:156:13)

用 Nodejs 编写的 API

数据库:mongoDB

控制器:

exports.product_details_by_name = function (req, res) {
    Product.findByProductName(req.params.name, function (err, product) {
        if (err){console.log(err); return next(err);}
        res.send(product);
    })
};

路由器

router.get('/test', product_controller.test);
router.post('/create', product_controller.product_create);
router.get('/:id', product_controller.product_details);
router.put('/:id/update', product_controller.product_update);
router.delete('/:id/delete', product_controller.product_delete);
router.get('/findByName', product_controller.product_details_by_name); // two get methods?

型号:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let ProductSchema = new Schema({
    name: {type: String, required: true, max: 100},
    price: {type: Number, required: true},
});

ProductSchema.findByProductName =  function(nam) { // is it right to do?
console.log("name"+nam);
  return this.find({ "name":  nam  });
};
// Export the model
module.exports = mongoose.model('Product', ProductSchema);

我正在接到邮递员的电话:

网址:localhost:8080/products/findByName

请求正文:

  {name: "name2"}

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您忘记在模型的 findbyProductName 函数中添加回调参数。

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    let ProductSchema = new Schema({
      name: {type: String, required: true, max: 100},
      price: {type: Number, required: true},
    });
    
    // Export the model
    let Product = module.exports = mongoose.model('Product', ProductSchema);
    
    exports.findByProductName =  function(nam, callback) { 
      console.log("name"+nam);
      Product.find({ "name":  nam  }).exec(callback);
    };
    

    【讨论】:

    • 让我编辑问题。我怀疑路由有问题,但我不确定并添加回调。
    • 我在路由进入错误路由之前遇到了同样的问题。你能像这样安排你的路线 router.get('/test', product_controller.test); router.get('/findByName', product_controller.product_details_by_name); router.get('/:id', product_controller.product_details); router.post('/create', product_controller.product_create); router.put('/:id/update', product_controller.product_update); router.delete('/:id/delete', product_controller.product_delete);
    • 是的。这解决了问题,但现在我遇到以下错误:Product.findByProductName is not a function
    • 尝试将模型中的 exports.findByProductName 链接到 module.exports.findByProductName
    • 谢谢,它成功了。但是我正在尝试将方法添加到像这里 mongoosejs.com/docs/guide.html 这样的模型中,但不确定这样做是否正确..
    猜你喜欢
    • 2014-06-20
    • 2020-11-19
    • 2021-07-04
    • 2017-08-18
    • 1970-01-01
    • 2016-11-11
    • 2017-03-26
    • 2017-05-26
    • 2020-05-20
    相关资源
    最近更新 更多