gpt4 book ai didi

javascript - Mongodb数据插入问题

转载 作者:行者123 更新时间:2023-11-30 19:13:58 24 4
gpt4 key购买 nike

当我尝试将任何内容插入 Mongo 数据库时出现此错误。任何帮助将不胜感激。

const mongoose = require('mongoose');
const dbpath = "mongodb+srv://cluster0-bm7js.mongodb.net/classic";

mongoose.connect(dbpath, {useUnifiedTopology: true , useNewUrlParser: true })
.then(()=> console.log("Now connected to MongoDB!"))
.catch(err=> console.error("Something went wrong", err));

const gameSchema = new mongoose.Schema( {
title: String,
publisher: String,
tags: [String],
date: {
type: Date,
default: Date.now
},
onSale: Boolean,
price: Number
});

const Game = mongoose.model('Game', gameSchema);

async function saveGame() {
const game = new Game( {
title: "Tekken 3",
publisher: "Neogeo",
tags: ["adventure", "action"],
onSale: false,
price: 69.99,
});

const result = await game.save();
console.log(result);

}

saveGame();

这是我的代码,运行上面的代码后我得到的错误是,

(node:94819) UnhandledPromiseRejectionWarning: MongoError: user is not allowed to do action [insert] on [classic.games]
at Connection.<anonymous> (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/pool.js:466:61)
at Connection.emit (events.js:198:13)
at processMessage (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/connection.js:364:10)
at TLSSocket.<anonymous> (/Users/thinkun/Desktop/mongo/node_modules/mongodb/lib/core/connection/connection.js:533:15)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:94819) 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(). (rejection id: 1)
(node:94819) [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.

不确定为什么 Mongo db 无法帮助我在内部工作。

提前致谢。

最佳答案

const mongoose = require('mongoose');
const dbpath = "mongodb+srv://cluster0-xxxx.mongodb.net/Game";

mongoose.connect(dbpath, {user: 'username', pass: 'password'})
.then(()=> console.log("Now connected to MongoDB!"))
.catch(err=> console.error("Something went wrong", err));

连接字符串有问题。全部按用户名和密码排序。

关于javascript - Mongodb数据插入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com