我已经在 mongodb atlas 上设置了必要的权限。我正在尝试从 node.js 写入数据库测试,这是我 products.js 中的代码:
router.post('/:productId', (req ,res ,next) => {
const id = req.params.productId;
Product.findById(id)
.exec()
.then(doc=> {console.log(doc);
res.status(200).json(doc).catch(err => console.log(err))
})
.catch(err =>{ console.log(err);
res.status(500).json({error: err});
})
});
终端显示错误:
UnhandledPromiseRejectionWarning: MongoError: user is not allowed to do action [insert] on [test.products]
at C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:598:61
at authenticateStragglers (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:516:16)
at Connection.messageHandler (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\pool.js:552:5)
at emitMessageHandler (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\connection.js:309:10)
at TLSSocket.<anonymous> (C:\Users\Hiary\Documents\rest-api\node_modules\mongodb-core\lib\connection\connection.js:452:17)
at TLSSocket.emit (events.js:180:13)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at TLSSocket.Readable.push (_stream_readable.js:218:10)
at TLSWrap.onread (net.js:581:20)
(node:21692) 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: 2)