Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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 E11000重复密钥错误集合:amaxon.products索引:name_1重复密钥:{name:“Nike SLim Shirt”}_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js E11000重复密钥错误集合:amaxon.products索引:name_1重复密钥:{name:“Nike SLim Shirt”}

Node.js E11000重复密钥错误集合:amaxon.products索引:name_1重复密钥:{name:“Nike SLim Shirt”},node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,以下是我的productModel.js中的productShema模型: import mongoose from 'mongoose' const productSchema = new mongoose.Schema( { name: { type: String, required: true, unique: true }, image: { type: String, required: true }, brand: { type: String, re

以下是我的
productModel.js
中的
productShema
模型:

import mongoose from 'mongoose'

const productSchema = new mongoose.Schema(
  {
    name: { type: String, required: true, unique: true },
    image: { type: String, required: true },
    brand: { type: String, required: true },
    category: { type: String, required: true },
    description: { type: String, required: true },
    price: { type: Number, required: true },
    countInStock: { type: Number, required: true },
    rating: { type: Number, required: true },
    numReviews: { type: Number, required: true },
  },
  {
    timestamps: true,
  }
)
const Product = mongoose.model('Product', productSchema)

export default Product
import express from 'express'
import Product from '../models/productModel.js'
import expressAsyncHandler from 'express-async-handler'
import data from '../data.js'

const productRouter = express.Router()

productRouter.get(
  '/',
  expressAsyncHandler(async (req, res) => {
    const products = await Product.find({})
    res.send(products)
  })
)

productRouter.get(
  '/seed',
  expressAsyncHandler(async (req, res) => {
    // await Product.remove({});
    const createdProducts = await Product.insertMany(data.products)
    res.send({ createdProducts })
  })
)

productRouter.get(
  '/:id',
  expressAsyncHandler(async (req, res) => {
    const product = await Product.findById(req.params.id)
    if (product) {
      res.send(product)
    } else {
      res.status(404).send({ message: 'Product not found' })
    }
  })
)

export default productRouter
这就是我在
productRouter.js中使用它的方式:

import mongoose from 'mongoose'

const productSchema = new mongoose.Schema(
  {
    name: { type: String, required: true, unique: true },
    image: { type: String, required: true },
    brand: { type: String, required: true },
    category: { type: String, required: true },
    description: { type: String, required: true },
    price: { type: Number, required: true },
    countInStock: { type: Number, required: true },
    rating: { type: Number, required: true },
    numReviews: { type: Number, required: true },
  },
  {
    timestamps: true,
  }
)
const Product = mongoose.model('Product', productSchema)

export default Product
import express from 'express'
import Product from '../models/productModel.js'
import expressAsyncHandler from 'express-async-handler'
import data from '../data.js'

const productRouter = express.Router()

productRouter.get(
  '/',
  expressAsyncHandler(async (req, res) => {
    const products = await Product.find({})
    res.send(products)
  })
)

productRouter.get(
  '/seed',
  expressAsyncHandler(async (req, res) => {
    // await Product.remove({});
    const createdProducts = await Product.insertMany(data.products)
    res.send({ createdProducts })
  })
)

productRouter.get(
  '/:id',
  expressAsyncHandler(async (req, res) => {
    const product = await Product.findById(req.params.id)
    if (product) {
      res.send(product)
    } else {
      res.status(404).send({ message: 'Product not found' })
    }
  })
)

export default productRouter
data.products
中,我有6个产品,然后我尝试从
http://localhost:5000/api/products/seed
此地址并向我显示:

“E11000重复密钥错误集合:amaxon.products索引:name_1重复密钥:{name:“Nike SLim Shirt”}”


当我试图从
http://localhost:5000/api/products
它只提供一种产品。如何修复此问题?

您正在尝试插入数据,而不是使用
查找
。选中此项:

productRouter.get(
  '/seed',
  expressAsyncHandler(async (req, res) => {
    // await Product.remove({});
    const createdProducts = await Product.find({})
    res.send({ createdProducts })
  })
)

由于
名称
属性设置为唯一,请确保:

  • 再次调用
    种子
    端点之前,删除
    产品mongo集合中存储的所有数据。(您已将其注释掉-
    等待产品。删除({});
  • data.js
    中的所有名称都是唯一的