Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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 学生验证失败:图像:需要路径“图像”_Node.js_Mongodb_Restapi - Fatal编程技术网

Node.js 学生验证失败:图像:需要路径“图像”

Node.js 学生验证失败:图像:需要路径“图像”,node.js,mongodb,restapi,Node.js,Mongodb,Restapi,我已经在rest API的帮助下使用node js上传了图像,当我们上传数据和图像时,postman给出了类似的错误,学生验证失败:需要图像:路径image。 我的邮递员回答是: { "errors": { "image": { "name": "ValidatorError", "message": "Path `image` is requir

我已经在rest API的帮助下使用node js上传了图像,当我们上传数据和图像时,postman给出了类似的错误,学生验证失败:需要图像:路径
image

我的邮递员回答是:

{
"errors": {
    "image": {
        "name": "ValidatorError",
        "message": "Path `image` is required.",
        "properties": {
            "message": "Path `image` is required.",
            "type": "required",
            "path": "image"
        },
        "kind": "required",
        "path": "image"
    }
},
"_message": "Student validation failed",
"name": "ValidationError",
"message": "Student validation failed: image: Path `image` is required."
}
我的模式是

const mongoose = require('mongoose');
const validator = require('validator');

const studentSchema = new mongoose.Schema({
 name: {
      type: String,
      required: true
 },
 email: {
      type: String,
      required: true,
      unique: [true, 'Email Id Already Exists..'],
      validate(value) {
           if (!validator.isEmail(value)) {
                throw new Error('Invalid Email');
           }
      }
 },
 contact: {
      type: Number,
      min: 10,
      required: true,
      unique: true,
 },
 address: {
      type: String,
      required: true,
 },
 image: {
      type: String,
      required: true,
 }, 

 })

    // we will create a new connection

      const Student = new mongoose.model('Student', studentSchema);
        module.exports = Student;
这是我的API

app.post('/students', upload.single('image'), async (req, res) => {     
 try {
      
      const user = new Student(req.body);
      console.log('req.body');
      const createStudent = await user.save();
      res.status(201).send(createStudent);
 } catch (e) {
      res.status(400).send(e);
 }
});

请帮助我,图像不包含在req.body中,如果您使用multer,您将在req.file中找到图像,控制台将其记录,然后将图像设置为req.file.path以将其存储到DB中

const user = new Student({
    ...req.body,
    req.file.path
})

我猜您正在使用
multer
上传文件

上传后,您将在
req.file
中获得文件的
url
。就我在任何文件上传程序包中看到的情况而言,
req.body
中没有该文件的数据

如果您使用的是multer,那么您可以在文档中找到它

您可以这样编写代码:

app.post('/students', upload.single('image'), async (req, res) => {     
 try {
      
      req.body.image = req.file.path
      const user = new Student(req.body);

      const createStudent = await user.save();
      res.status(201).send(createStudent);
 } catch (e) {
      res.status(400).send(e);
 }
});

先生,在我们添加代码的地方,bcz i new to nodead,而不是您的初始化:
const user=new Student(req.body)
@Nenad Milosavljevic,先生,我已经尝试过了,但是它在邮递员响应中给出了相同的消息declare,Sir const user=new Student(req.body,req.file.path);可以吗,Sir const user=新学生(req.body,req.file.path);可以吗?我已经更新了答案并添加了代码。如果这有帮助,那就考虑接受我的回答。