Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js ValidationError:产品验证失败:年龄:路径'age'是必需的,名字:路径'first\u name'是必需的_Node.js_Mongodb_Mongoose_Node Modules - Fatal编程技术网

Node.js ValidationError:产品验证失败:年龄:路径'age'是必需的,名字:路径'first\u name'是必需的

Node.js ValidationError:产品验证失败:年龄:路径'age'是必需的,名字:路径'first\u name'是必需的,node.js,mongodb,mongoose,node-modules,Node.js,Mongodb,Mongoose,Node Modules,我是Node.js新手,我知道这是一个非常常见的问题,很多人都问过同样的问题。但你知道,在我的情况下,所有的解决方案都不起作用请看一下代码,如果我遗漏了什么,请告诉我谢谢 这是node.js应用程序与Mongo db交互。 Product.controllers.js const Products = require("../models/Product.models"); exports.Product_create = function(req, res, next){ conso

我是Node.js新手,我知道这是一个非常常见的问题,很多人都问过同样的问题。但你知道,在我的情况下,所有的解决方案都不起作用

请看一下代码,如果我遗漏了什么,请告诉我

谢谢

这是node.js应用程序与Mongo db交互。

Product.controllers.js

const Products = require("../models/Product.models");

exports.Product_create = function(req, res, next){
    console.log(req.body.Name); //For test only - Here I am getting the values
    console.log(req.body.Age);  //For test only - Here I am getting the values
    let newProduct = new Products()
    {
        first_name = req.body.Name,
        age = req.body.Age
    };
    newProduct.save(function (err) {
        if (err) {
            return next(err);
        }
        res.send('Product Created successfully')
    })
};

exports.test = function(req, res){
    console.log("test controller reached successfully!");
};

Product.models.js


这是我发出的请求。


我在
console.log(req.body.Name)中接收到正确的输出
控制台日志(请求主体年龄)。但它仍然没有保存数据。

错误在产品初始化中

const Products = require("../models/Product.models");

exports.Product_create = function(req, res, next){
    console.log(req.body.Name); //For test only - Here I am getting the values
    console.log(req.body.Age);  //For test only - Here I am getting the values
    let newProduct = new Products(
    {
        first_name:req.body.Name,
        age:req.body.Age
    });
    newProduct.save(function (err) {
        if (err) {
            return next(err);
        }
        res.send('Product Created successfully')
    })
};

exports.test = function(req, res){
    console.log("test controller reached successfully!");
};

您需要提供
first\u name和age
作为键值对

谢谢,这里的错误是让newProduct=newproducts(){first\u name:req.body.name,age:req.body.age};年龄:。它在说“;”预期的。谢谢:),告诉你这可能是一些简单而愚蠢的事情:)已经消除了错误,但数据仍然没有保存在数据库中。Mongo中创建了空数据库。您可以尝试console.log(err)并检查保存时是否有任何错误吗
const Products = require("../models/Product.models");

exports.Product_create = function(req, res, next){
    console.log(req.body.Name); //For test only - Here I am getting the values
    console.log(req.body.Age);  //For test only - Here I am getting the values
    let newProduct = new Products(
    {
        first_name:req.body.Name,
        age:req.body.Age
    });
    newProduct.save(function (err) {
        if (err) {
            return next(err);
        }
        res.send('Product Created successfully')
    })
};

exports.test = function(req, res){
    console.log("test controller reached successfully!");
};