首页 > 解决方案 > 在副本集配置中找不到名为“majority/shopping_mern_app”的写入关注模式

问题描述

大家好,我在向数据库发出发布请求时收到一条错误消息,但我正在将文档添加到数据库中,我只是收到这条消息“在副本集配置中找不到名为‘majority/shopping_mern_app’的写关注模式” ,虽然我能够发出 post 请求但收到错误消息我正在成功发出 get 请求

import express from "express";
import mongoose from "mongoose";
import path from "path";
import cors from "cors";
import morgan from "morgan";
import config from "./config/index.js";

// routes
import itemRoutes from "./routes/items.js";

const app = express();

// Logger Middleware
app.use(morgan("common"));
// Bodyparser Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// CORS Middleware
app.use(cors());

// DB Config
const { MONGO_URI, MONGO_DB_NAME } = config;
const db = `${MONGO_URI}/${MONGO_DB_NAME}`;

// Connect to Mongo
mongoose
  .connect(db) // Adding new mongo url parser
  .then(() => console.log("MongoDB Connected..."))
  .catch((err) => console.log(err));

// Use Routes
app.use("/api/items", itemRoutes);

// Serve static assets if in production
if (process.env.NODE_ENV === "production") {
  // Set static folder
  app.use(express.static("client/build"));

  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

export default app;

import app from "./app.js";
import config from "./config/index.js";

const { PORT } = config;

app.listen(PORT, () => console.log(`Server started on PORT ${PORT}`));

import dotenv from "dotenv";

dotenv.config();

export default {
  PORT: process.env.PORT || 5000,
  MONGO_URI:
    process.env.MONGO_URI ||
    "mongodb+srv://myusername:mypassword@cluster0.3zi2u.mongodb.net/shopping_mern_app?retryWrites=true&w=majority",
  MONGO_DB_NAME: process.env.MONGO_DB_NAME || "shopping_mern_app",
};

export const post_items = async (req, res) => {
  const newItems = await Items({ name: req.body.name });
  try {
    if (!newItems) throw Error("Something went wrong saving the item");
    await newItems.save();

    res.status(200).json({
      message: "Successfully posted items to the database",
      items: newItems,
    });
  } catch (error) {
    res.status(400).json({
      Error: "Error while posting items",
      message: error.message,
    });
  }
};

标签: node.jsexpressmongoose

解决方案


推荐阅读