2017-05-12 4 views
0

mongoDBデータベースにドキュメントを追加するときに何が起こっているのかを調べようとしています。mongoDBデータベースにドキュメントを追加できません。エラーが発生しました:パス名が必要です

私はこのエラーを取得していた文書を追加しようとすると:だから私はこのアクションを実行するためにエクスプレスのJsとマングースを使用してい

{ 
"errors": { 
"name": { 
"message": "Path `name` is required.", 
"name": "ValidatorError", 
"properties": { 
"type": "required", 
"message": "Path `{PATH}` is required.", 
"path": "name" 
    }, 
"kind": "required", 
"path": "name" 
    } 
    }, 
"message": "Product validation failed", 
"name": "ValidationError" 
} 

。私のモデルで :product.js私のAPIルートで


var mongoose = require('mongoose'); 

var productSchema = new mongoose.Schema({ 
name: {type: String, required: true}, 
category: {type: String, default: ''}, 
price: { type: Number, default: 0}, 
picture: { type: String}, 
quantity: {type: Number, default: 0}, 
status: { 
    type: String, 
    enum: ['pending', 'In Progress', 'Cancelled', 'Done'], 
    default: 'pending' 
}, 
date: { type: Date, default: Date.now}, 
description: { type: String}, 
owner: {type: String} 
}); 

var Product = mongoose.model('Product', productSchema); 

module.exports = Product; 

、私はこのアクションを実行するためのパスを定義します。私のコントローラファイル内 index.js最後

var express = require('express'); 
var router = express.Router(); 
var productCtrl = require('../controllers/productCtrl'); 

router.get('/products', productCtrl.getAllProducts); 
router.get('/products/:productId', productCtrl.readProduct); 
router.post('/products', productCtrl.createProduct); 
router.delete('/products/:productId', productCtrl.removeProduct); 
router.put('/products/:productId', productCtrl.updateProduct); 

module.exports = router; 

、 : productCtrl.js

var Product = require ('../models/products'); 
var sendJsonResponse = function(res, status, content) { 
    res.status(status); 
    res.json(content); 
} 
module.exports.createProduct = function (req, res){ 
Product 
     .create({ 
     name: req.body.name, 
     category: req.body.category, 
     price: req.body.price, 
     picture: req.body.picture, 
     quantity: req.body.quantity, 
     status: req.body.status, 
     date: req.body.date, 
     description: req.body.description, 
     owner: req.body.owner 

     }, function createProduct(err, product){ 
     if(err){ 
      sendJsonResponse(res, 404, err); 
      return; 
     } 
     else { 
      sendJsonResponse(res, 201, product); 
     } 
     }); 
} 

行動

node.js version: 6.9.4 
mongodb version: 3.4.4 
express version: ~4.13.4 
mongoose version: ^4.9.8 

は、私は本当に例外はそのフィールド「名前」を意味みんな

答えて

1

を助ける必要があります、例外は、私は、私が使用している製品のドキュメントは、私のエンバイロメントについて

JSON形式で返さなければならないということですお使いのモデルに(あなたが設定=真必要)必要ですが、提供していない、あなたはそれにnullまたはundefined渡す意味

.create({ 
    name: req.body.name, 

さreq.body.nameはおそらく定義されていません。あなたのPOSTアクションを確認し、console.log(req.body)を試してみてください。

1

例外は、モデルのフィールド "name"が必須であること(required = trueを設定)、req.body.name POSTアクションを確認することを意味します。

この問題を回避するためにデータの検証とサニタイズの両方を実行するExpress Validatorモジュール。私はこのアクションをテストするために郵便配達を使用していたときに、実際に
productCtrl.js

var Product = require ('../models/products'); 
var sendJsonResponse = function(res, status, content) { 
    res.status(status); 
    res.json(content); 
} 
module.exports.createProduct = function (req, res){ 



    var new_product = new Product();//instance of object (model) product 

     new_product.name: req.body.name, 
     new_product.category: req.body.category, 
     new_product.price: req.body.price, 
     new_product.picture: req.body.picture, 
     new_product.quantity: req.body.quantity, 
     new_product.status: req.body.status, 
     new_product.date: req.body.date, 
     new_product.description: req.body.description, 
     new_product.owner: req.body.owner 

    new_product.save(function(err, addprod) {//save your new product in database 

    if (err){ 

    res.send(err);// error when save 

    } 

    res.send(addprod);//else return a product add 

    }); 

    }; 
+0

:次のように:

req.checkBody('name', 'Product name required').notEmpty(); var errors = req.validationErrors(); //Run the validators var errors = req.validationErrors(); if (errors) { //If there are errors data form invalid res.send(''); return; } else { // Data from form is valid. } 

あなたはこれを試すことができます。私は製品モデルのすべてのフィールドを埋める。私はこれを試し、私はあなたに何を伝えます。あなたの助けをしてくれてありがとう –

+0

よろしくお願いします!私はあなたの帰りを待っています! –

+0

今はすべてうまく動作します。しかし、私は理由を知らない...私はあなたのapprochを試みるが、私は同じエラーに直面していた。だから私は単に私のサーバー内のすべてのデータを削除し、私は私のアプリを再起動し、それは動作します... –

関連する問題