对MEAN的400错误请求

时间:2017-11-12 17:03:39

标签: javascript angular express mean

所以经过一个小时的研究后我发布了这个问题....这就是浏览器的答案,我找不到任何理由...... 因此,登录后,我想获取记录的用户的分数。分数位于用户表(Mongo ...)的子表中。但是对服务器的调用不起作用。这是我的代码:

login.component.ts

this.dbService.scoresDownload(this.username).subscribe(scoresDownload => {
                console.log(scoresDownload);
              });

db.service.ts

  scoresDownload(username) {
this.createAuthenticationHeaders();
console.log(username)
return this.http.post(this.domain + 'training/scoresDownload', username, this.options).map(res => res.json());

}

training.js(快递路线)

          router.post('/scoresDownload', (req, res) => {

// let wordScoresDb = [];
console.log(req)

User.find({}, (err, result) => {
  if (err) {
    res.json({ success: false, message: err }); // Return connection error
  } else {
    res.json({ success: true, message: result }); // Return as vailable username
  }
})});

如果我使用get而不是帖子,它的效果很好....而最棘手的部分是它可以与我所有的其他调用一起使用。所以,我真的不知道我搞砸了哪里...... 干杯啦!

编辑: 我忘了提及快递发送的错误

SyntaxError: Unexpected token # in JSON at position 0
at Object.parse (native)
at createStrictSyntaxError (D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\types\json.js:157:10)
at parse (D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\types\json.js:83:15)
at D:\Code\Deutsch_App_MEAN\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:224:16)
at done (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (D:\Code\Deutsch_App_MEAN\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)

1 个答案:

答案 0 :(得分:0)

好的......我悲惨地找到了答案......我通过MEAN传递的用户名是一个字符串......显然,http模块中的“post”只接受对象或数组,而不是字符串...... Voilà:)