节点JS / mongoose后端在html中返回错误响应

时间:2018-05-07 12:25:58

标签: node.js mongoose

我使用/ signup端点创建了一个带有mongoose的节点JS后端来注册用户。现在我用角度前端调用BE。当没有错误时,一切似乎都运行得很好。但是,当我尝试创建一个重复的电子邮件地址(从而生成错误)时,我得到一个HTML格式的响应?我不知道如何处理。我的代码

FE(角度5):

public signUp(data: RegistrationParams): Observable<any> {
    return this._http.post(this._actionUrl, JSON.stringify(data), this._requestOptions)
      .map((response: any) => {
        return JSON.parse(response._body);
      })
      .catch((e) => {
        console.log('error:', e);

      });
  }

这会在错误上记录以下内容:

 <html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Error: something went wrong while adding data to the database E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }<br> &nbsp; &nbsp;at SignUpController.&lt;anonymous&gt; (/usr/src/app/src/api/v1/SignupController.ts:29:13)<br> &nbsp; &nbsp;at throw (native)<br> &nbsp; &nbsp;at rejected (/usr/src/app/src/api/v1/SignupController.ts:17:65)<br> &nbsp; &nbsp;at process._tickCallback (internal/process/next_tick.js:109:7)</pre>
</body>
</html>

BE(nodeJS):

@JsonController()
@Service()
export class SignUpController {
  @Post('/signup')
  public async addUser(@Body() body: any, @Res() resp: Response): Promise<T> {
    const newUser = new USER(body);
    try {
      await newUser.save();
      return <DatabaseResponse>{
        successful: true,
        message: 'succes',
      };
    } catch (err) {
      throw new Error('something went wrong while adding data to the database ' + err.message);
    }
  }
}

这只是我尝试抛出错误的方法之一,但一切都在用html响应。那是对的吗?不知道我在这里做错了什么。

编辑:

服务器出错:

{ WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }","op":{"_id":"5af049f9312e7c07252eaf64","email":"mischa.boldyISC@nl.ibm.com","firstName":"weqwewq","lastName":"qwqew","phoneNumber":"12345663343","plateNumber":"123456","role":"User","__v":0}})
    at /usr/src/app/node_modules/mongodb/lib/bulk/unordered.js:528:15
    at handleCallback (/usr/src/app/node_modules/mongodb/lib/utils.js:128:55)
    at resultHandler (/usr/src/app/node_modules/mongodb/lib/bulk/unordered.js:454:5)
    at /usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:541:18
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  name: 'BulkWriteError',
  message: 'E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }',
  driver: true,
  code: 11000,
  index: 0,
  errmsg: 'E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function],
  result: 
   BulkWriteResult {
     ok: [Getter],
     nInserted: [Getter],
     nUpserted: [Getter],
     nMatched: [Getter],
     nModified: [Getter],
     nRemoved: [Getter],
     getInsertedIds: [Function],
     getUpsertedIds: [Function],
     getUpsertedIdAt: [Function],
     getRawResponse: [Function],
     hasWriteErrors: [Function],
     getWriteErrorCount: [Function],
     getWriteErrorAt: [Function],
     getWriteErrors: [Function],
     getLastOp: [Function],
     getWriteConcernError: [Function],
     toJSON: [Function],
     toString: [Function],
     isOk: [Function] } }
add obj 2 E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }
Error: something went wrong while adding data to the database E11000 duplicate key error collection: testdb.users index: user.email_1 dup key: { : null }
    at SignUpController.<anonymous> (/usr/src/app/src/api/v1/SignupController.ts:29:13)
    at throw (native)
    at rejected (/usr/src/app/src/api/v1/SignupController.ts:17:65)
    at process._tickCallback (internal/process/next_tick.js:109:7)

1 个答案:

答案 0 :(得分:0)

您的用户模型可能包含电子邮件字段,请确保唯一字段为false,但不允许重复输入。

email:{
        type:String,
        required:true,
        unique:true,
        trim:true
    }