2017-03-24 51 views
0

我使用Express,Graphql(Apollo服務器)和貓鼬製作API服務器。防止與Mongoose,節點和graphql控制檯錯誤

我正在測試用戶創建。當郵件被複制時,mongoose會拋出一個錯誤(Validation error。Unique = true),並且graphql處理非常好。但控制檯(終端)也顯示錯誤。我怎樣才能防止這個問題?

解析器:。

const MODEL_PATH = '../../models'; 
const User = require(MODEL_PATH + '/User'); 
const { register } = require('../../controllers/auth/RegisterController'); 

module.exports = { 
    RootQuery: { 
    users() { 
     return User.find({}); 
    } 
    }, 
    Mutation: { 
    registerUser(_, data) { 
     return register(data); 
    } 
    } 
}; 

RegisterController(註冊功能)

exports.register = function(data) { 
    const { email, password } = data; 

    const user = new User({ 
    email, 
    password 
    }); 

    return new Promise((resolve, reject) => { 
    user.save().then((user) => { 
     resolve(user); 
    }).catch((err) => { 
     reject(err); 
    }); 
    }); 

}; 

並在控制檯中的錯誤(我不希望我處理的錯誤在控制器我想要的ERROR僅在GRAPHQL RESPONSE)在Graphiql(即確定)

MongoError: E11000 duplicate key error collection: y.users index: email_1 dup key: { : "[email protected]" } 
    at Function.MongoError.create (/Volumes/Datos/x/code/y/server/node_modules/mongodb-core/lib/error.js:31:11) 
    at toError (/Volumes/Datos/x/code/y/server/node_modules/mongodb/lib/utils.js:114:22) 
    .... 

響應

{ 
    "data": { 
    "registerUser": null 
    }, 
    "errors": [ 
    { 
     "message": "E11000 duplicate key error collection: y.users index: email_1 dup key: { : \"[email protected]\" }", 
     "locations": [ 
     { 
      "line": 9, 
      "column": 3 
     } 
     ], 
     "path": [ 
     "registerUser" 
     ] 
    } 
    ] 
} 

謝謝

回答