【问题标题】:POST data with user details in mongo db using node.js使用 node.js 在 mongodb 中发布带有用户详细信息的数据
【发布时间】:2019-03-12 20:03:05
【问题描述】:

我正在使用带有 Angular 6 的 MEAN 堆栈开发一个 Web 应用程序。我有一个表单可以将数据提交到 MongoDB。以下是保存功能,它可以工作。 它将挤出的值保存在数据库中。

saveExtrudedHeightValue(extrudedHeight: NgForm) {
  if (extrudedHeight.value != "" && extrudedHeight.value != null) {
    this.extrudedHeightService.saveExtrudedHeight(extrudedHeight.value).subscribe(res => {
      console.log(res);
    }, (err) => {
      console.log(err);
    });
  }
}

这是模型

// Schema for extruded height panel
var extrudedHeightSchema = new mongoose.Schema({
  userName: {
    type: String
  },
  extrudedHeight: {
    type: Number
  },

});
module.exports = mongoose.model('extrudedHeightValue', extrudedHeightSchema);

这是我的发帖路线

//post extrudedHeight values
router.post("/save", function(req, res) {
  var mod = new extrudedHeight(req.body);

  extrudedHeight.findOneAndUpdate({
      userName: req.body.email,
      extrudedHeight: req.body.extrudedHeight,
    },
    req.body, {
      upsert: true,
      new: true
    },
    function(err, data) {
      if (err) {
        console.log(err);
        res.send(err);
      } else {

        res.send(mod);
      }
    }
  );
});

这里是服务。

// service for save extruded height 
saveExtrudedHeight(extrudedHeight): Observable < any > {
  return this.http.post('/extrudedHeight/save', extrudedHeight, httpOptions)
    .pipe(
      catchError(this.handleError)
    );
}

现在我想用当前用户的userName 将数据保存在数据库中。我可以通过这个检索当前用户的userNamethis.payload.user['email'] 我的问题是我不知道如何通过此userName 发布路由以保存在数据库中。

这是我获得令牌的地方。

this.authService.onTokenChange().subscribe(
  (token: NbAuthJWTToken) => {
    if (token.isValid()) {
      this.user = token.getPayload().user;
      this.payload = token.getPayload();
      console.log(this.payload.user['email']);
    }
  }
)

【问题讨论】:

  • 这里的payload 是什么?你从哪里得到它?它属于哪个文件?
  • 它从令牌检索文件中获取。该部分返回正确的电子邮件
  • 但是你是怎么得到它的?有没有为它写的服务?如果有,您可以在该服务中创建一个方法,该方法将返回一个 Observable。然后,您可以订阅该 observable 以检索电子邮件,然后将其作为有效负载的一部分发送到 /extrudedHeight/save
  • 如何传递/extrudedHeight/save?可以举个例子解释一下吗?打扰了。
  • 请为您在上面评论中提到的令牌检索文件添加一些代码。

标签: node.js angular express mean-stack


【解决方案1】:

您可以先在saveExtrudedHeight 方法内调用this.authService.onTokenChange,然后使用flatMap 运算符解开将由http.post 返回的内部Observable

这将转化为这样的代码:

import { flatMap } from 'rxjs/operators';
import { throwError } from 'rxjs';

...

saveExtrudedHeight(extrudedHeight): Observable<any> {
  const requestPayload = {
    extrudedHeight
  };
  return this.authService.onTokenChange()
    .pipe(flatMap(
    (token: NbAuthJWTToken) => {
      if (token.isValid()) {
        this.user = token.getPayload().user;
        this.payload = token.getPayload();

        const email = this.payload.user['email'];
        requestPayload.email = email;

        // Make the changes here to send the email as the Request Payload.

        return this.http.post('/extrudedHeight/save', requestPayload, httpOptions)
          .pipe(
            catchError(this.handleError)
          );
      } else {
        throwError('Something went wrong!');
      }
    }
  ));
}

PS:我不确定这是否可行,因为我尚未对其进行测试,而且如果没有最低限度的 StackBlitz 工作,我就无法做到。

【讨论】:

  • 感谢您的建议。但是如何发送电子邮件以保存在数据库中
  • 我已经更新了我的答案。请看一看。
猜你喜欢
  • 2014-06-01
  • 1970-01-01
  • 2012-04-03
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
相关资源
最近更新 更多