Javascript 当我试图保存Mongoose模式时,它不起作用
这是我正在导出的代码 这是当我尝试执行wait carritoschema.save()时出错的代码发生以下错误 这是错误,但我不知道如何修复它Javascript 当我试图保存Mongoose模式时,它不起作用,javascript,node.js,mongoose,Javascript,Node.js,Mongoose,这是我正在导出的代码 这是当我尝试执行wait carritoschema.save()时出错的代码发生以下错误 这是错误,但我不知道如何修复它 (节点:4372)未处理的PromisejectionWarning:MongooseError:Operation`carritos.insertOne()`缓冲在10000ms后超时 超时时。(C:\Users\reneo\Desktop\shopingWebsite\backend\node\u modules\mongoose\lib\driv
(节点:4372)未处理的PromisejectionWarning:MongooseError:Operation`carritos.insertOne()`缓冲在10000ms后超时
超时时。(C:\Users\reneo\Desktop\shopingWebsite\backend\node\u modules\mongoose\lib\drivers\node mongodb native\collection.js:184:20)
在listOnTimeout(internal/timers.js:554:17)
在processTimers(internal/timers.js:497:7)
(使用`node--trace warnings…`显示警告的创建位置)
(节点:4372)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于抛出异步函数的内部
没有catch块,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:4372)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
首先需要配置数据库连接。有一个链接将帮助您:
如果您有任何其他疑问,请告诉我。您首先需要配置数据库连接。有一个链接将帮助您:
如果您有任何其他疑问,请告诉我。我也遇到了同样的错误。我将数据库连接方法从
mongoose.Createconnection(…)
更改为mongoose.connect(…)
,并且我可以保存到数据库。我收到了相同的错误。我将数据库连接方法从mongoose.Createconnection(…)
更改为mongoose.connect(…)
我可以保存到数据库。发生此错误的原因是您必须将IP地址设置为网络选项卡中MongoDB Atlass的任何位置发生此错误的原因是您必须将IP地址设置为网络选项卡中MongoDB Atlass的任何位置您是救世主您是救世主
const mongoose = require("mongoose");
const carrito = new mongoose.Schema({
nombre: String,
precio: Number
})
module.exports = mongoose.model("carrito",carrito);
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");
});
(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.