简体   繁体   English

使用 postman 将 nodejs 上的多个数据发送到 mongodb

[英]Send multiple data on nodejs to mongodb using postman

So, I'd like to send multiple data(json) using postman.所以,我想使用 postman 发送多个数据(json)。 for example I've got two data json.例如,我有两个数据 json。 I wanna send that json to mongodb using postman at same time like that.我想同时使用 postman 将 json 发送到 mongodb 。 But I got an error但我有一个错误

SyntaxError: Unexpected token , in JSON at position 168<br> and so on.

So I've tried wrap that object to the array.所以我尝试将 object 包装到数组中。 And I got an error我得到了一个错误

Products validation failed: title: Path `title` is required., type: Path `type` is required., description: Path `description` is required., filename: Path `filename` is required., price: Path `price` is required., rating: Path `rating` is required.

I'm sure all the data has filled.我确定所有数据都已填满。

My goal I wanna send a lot of data json like that.我的目标是像这样发送大量数据 json 。 Not just one or two.不只是一两个。 Like ten or more but just once send.喜欢十个或更多,但只发送一次。

Example json I want to send to mongodb示例 json 我想发送到 mongodb

{
  "title": "Asparagus",
  "type": "vegetable",
  "description": "Asparagus with ham on the wooden table",
  "filename": "2.jpg",
  "price": "18.95",
  "rating": "3"
}, {
  "title": "Green smoothie",
  "type": "dairy",
  "description": "Glass of green smoothie with quail egg's yolk, served with cocktail tube, green apple and baby spinach leaves over tin surface.",
  "filename": "3.jpg",
  "price": "17.68",
  "rating": "4"
}

schema图式

const productSchema = new mongoose.Schema({
    title: {type: String, required: true},
    type: {type: String, required: true},
    description: {type: String, required: true},
    filename: {type: String, required: true},
    price: {type: Number, required: true},
    rating: {type: Number, required: true},
    date: {type: Date, default: Date.now}
});

Routes (post route)路线(邮政路线)

const Product = require('../models/product.js');
routes.post('/', async (req, res) => {
  const product = new Product({
    title: req.body.title,
    type: req.body.type,
    description: req.body.description,
    filename: req.body.filename,
    price: req.body.price,
    rating: req.body.rating
  });
  try {
    const new_product = await product.save();
    return res.status(201).json(new_product);
  }catch(err){
    return res.status(400).json({error: err.message});
  }
});

Thank you谢谢

Apologies if you have thought of this, but it's possible to pass an array in Postman then have your route.forEach on the elements of the array to store in the database.抱歉,如果您想到了这一点,但是可以在 Postman 中传递一个数组,然后将您的 route.forEach 放在数组的元素上以存储在数据库中。

You could even set a conditional to check if it's an array first if you sometimes want to send a single object.如果您有时想要发送单个 object,您甚至可以设置一个条件来首先检查它是否是一个数组。

You will have to send the JSON as an array.您必须将 JSON 作为数组发送。

[{
  "title": "Asparagus",
  ...
}, {
  "title": "Green smoothie",
  ...
}]

Then you can access it like this:然后你可以像这样访问它:

req.body.forEach(item => {
  // do something with item
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM