简体   繁体   中英

Mongoose schema does not work when I try to save it

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.org/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.

You need first of all configurate the database connection. There's a link that will help you: https://kb.objectrocket.com/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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