这是什么错误

这是什么错误

(node:23028) UnhandledPromiseRejectionWarning: MongoError: E11000 duplicate key error collection: comment2.users index: Username_1 dup key: { Username: null }

    at Function.create (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\core\error.js:51:12)

    at toError (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\utils.js:123:22)

    at C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\operations\common_functions.js:265:39

    at handler (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\core\sdam\topology.js:913:24)

    at C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\cmap\connection_pool.js:356:13

    at handleOperationResult (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\core\sdam\server.js:492:5)

    at MessageStream.messageHandler (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\cmap\connection.js:272:5)

    at MessageStream.emit (events.js:315:20)

    at processIncomingData (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\cmap\message_stream.js:144:12)

    at MessageStream._write (C:\Users\Administrator\Desktop\作业\mongo测试\test4\node_modules\_mongodb@3.5.10@mongodb\lib\cmap\message_stream.js:42:5)

    at doWrite (_stream_writable.js:403:12)

    at writeOrBuffer (_stream_writable.js:387:5)

    at MessageStream.Writable.write (_stream_writable.js:318:11)

    at Socket.ondata (_stream_readable.js:717:22)

    at Socket.emit (events.js:315:20)

    at addChunk (_stream_readable.js:295:12)

(node:23028) 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:23028) [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.


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕码 2020-08-10 10:42:18

同学你好,如果想要重复新增同一条数据,需要进行如下修改:

1、将model.js代码中username下的unique属性注释掉:

http://img1.sycdn.imooc.com//climg/5f30b3c80911a37704090130.jpg

2、在compass中将username对应的主键删除:

http://img1.sycdn.imooc.com//climg/5f30b3e00969c70609750306.jpg

http://img1.sycdn.imooc.com//climg/5f30b3f10936e3a404100222.jpg

祝学习愉快~

好帮手慕码 2020-08-09 18:04:12

同学你好,查看报错:

http://img1.sycdn.imooc.com//climg/5f2fc8f9090f150e08050107.jpg

报错表示在插入数据时出现id重复错误,请问同学是重复新增同一个数据吗?可以在compass把该条数据删除掉后再新增试下:

http://img1.sycdn.imooc.com//climg/5f2fc9cd09e9499209920201.jpg

祝学习愉快~

  • 提问者 慕雪9296518 #1
    不是这个问题,我在chns里询问过了,问题是出在变量重复save(),然后接下来每次都出现错误,还像是系统自主添加_id出现的错误,只能靠删除集合才可以解决,我想问问这种问题该怎么解决
    2020-08-09 21:56:52
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
5. Node.js全栈开发
  • 参与学习           人
  • 解答问题       307    个

前端工程师会后端开发已是趋势。从node.js到koa2再到数据库知识。结合真实项目,从项目需求分析、接口和数据设计到业务开发,帮你打通前后端任督二脉,为就业加码。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师