最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose schema does not work when I try to save it - Stack Overflow

programmeradmin0浏览0评论

this is the code that i am exporting

const mongoose = require("mongoose");
const carrito = new mongoose.Schema({
    nombre: String,
    precio: Number
})
module.exports = mongoose.model("carrito",carrito);

this is the code where the error occurs, when i try to do the await carritoschema.save(); the error below happen

const express = require("express");
const bodyparser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
const carrito = require("./carrito");
const cors = require("cors");
const { json } = require("body-parser");
app.use(cors());
app.use(express.json());

app.post("/",async (req,res)=>{
    
    let {nombre,precio} = req.body;
    console.log(nombre +" "+ precio);

    let carritoschema = new carrito();
    carritoschema.nombre= nombre;
    carritoschema.precio = precio;
    await carritoschema.save();

    //await carritoschema.find(); 
});
app.get("/carrito",async (req,res)=>{ 
    
})
 

//app.get("/",async(req,res)=>{});
 
app.listen(5000,()=>{
    console.log("listening on port 5000"); 
});

this is the error but i have no idea how to fix it

(node:4372) UnhandledPromiseRejectionWarning: MongooseError: Operation `carritos.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\reneo\Desktop\shopingWebsite\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:184:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4372) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function 
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 1)     
(node:4372) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

this is the code that i am exporting

const mongoose = require("mongoose");
const carrito = new mongoose.Schema({
    nombre: String,
    precio: Number
})
module.exports = mongoose.model("carrito",carrito);

this is the code where the error occurs, when i try to do the await carritoschema.save(); the error below happen

const express = require("express");
const bodyparser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
const carrito = require("./carrito");
const cors = require("cors");
const { json } = require("body-parser");
app.use(cors());
app.use(express.json());

app.post("/",async (req,res)=>{
    
    let {nombre,precio} = req.body;
    console.log(nombre +" "+ precio);

    let carritoschema = new carrito();
    carritoschema.nombre= nombre;
    carritoschema.precio = precio;
    await carritoschema.save();

    //await carritoschema.find(); 
});
app.get("/carrito",async (req,res)=>{ 
    
})
 

//app.get("/",async(req,res)=>{});
 
app.listen(5000,()=>{
    console.log("listening on port 5000"); 
});

this is the error but i have no idea how to fix it

(node:4372) UnhandledPromiseRejectionWarning: MongooseError: Operation `carritos.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\reneo\Desktop\shopingWebsite\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:184:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4372) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function 
without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)     
(node:4372) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Share Improve this question edited Dec 22, 2020 at 16:23 peterh 1 asked Dec 4, 2020 at 20:59 Rene avilaRene avila 211 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You need first of all configurate the database connection. There's a link that will help you: https://kb.objectrocket./mongo-db/simple-mongoose-and-node-js-example-1007 .

If you have any other doubt let me known.

I was getting the same error. I changed my database connection method from mongoose.Createconnection(...) to mongoose.connect(...) and I could save to the database.

This error occurs because you have to set IP address to anywhere in your MongoDB Atlass in network tab

发布评论

评论列表(0)

  1. 暂无评论