V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
salamanderMH
V2EX  ›  问与答

Nodejs mongoose 库中 MongoError 问题?

  •  
  •   salamanderMH · May 16, 2019 · 945 views
    This topic created in 2542 days ago, the information mentioned may be changed or developed.

    描述

    我想显式触发一个重复插入错误: E11000 duplicate key error collection: my_data.user index: uid_1 dup key: { : "111247326728" } (其实就是唯一索引重复插入问题)

    const MongoError = require('mongodb-core').MongoError
    
    async function insertUser(uid) {
        try {
            await userModel.create({
                "uid": uid,
                "create_datetime": new Date(),
            })
        } catch (e) {
            console.log(e instanceof MongoError)
            if (e.code === 11000) {
                console.log('重复插入错误')
            }
        }
    }
    

    然而我发现e instanceof MongoError结果是 false MongoError 是我从报错的 stackTrace 里找出来的

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   804 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 22:33 · PVG 06:33 · LAX 15:33 · JFK 18:33
    ♥ Do have faith in what you're doing.