node.js - 在副本集配置中找不到名为“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,
});
}
};
解决方案
推荐阅读
- php - 当我在 Wordpress 中更改它时出现永久链接错误 404
- reactjs - React .map 每次更新时都会渲染整个数组并导致重复
- javascript - 嗨,我想创建一个表单,当有人选择一个选项时它会显示产品图片
- node-red - 节点红色功能等待 2 个输入
- jwt - 通过 Apache Knox 网关访问 Apache NIFI REST API (jwt)
- r - ggplot2 对象不是在修改时复制?
- python-3.x - 在噪声 (rand) fnc w fsolve 中找到根(ValueError:函数返回的数组在调用之间改变了大小)
- python - 如何正确填充缓冲区?
- python - 安装 PyTorch 后 conda list 返回运行时错误 Path not Found
- javascript - 在 JavaScript 中组合两个对象?