Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在我刷新页面之前,不会显示创建的对象_Node.js_Mongodb_Angular_Express_Mean Stack - Fatal编程技术网

Node.js 在我刷新页面之前,不会显示创建的对象

Node.js 在我刷新页面之前,不会显示创建的对象,node.js,mongodb,angular,express,mean-stack,Node.js,Mongodb,Angular,Express,Mean Stack,请帮助,我有一个消息服务,它从数据库获取我的消息,并将它们显示在用户配置文件页面上。创建或删除邮件时,我只能在刷新浏览器时看到更改 在我应用过滤器按Id排序之前,页面会动态更新,为什么过滤器会取消绑定我的消息数组 配置文件组件: export class ProfileComponent implements OnInit { user: User; userId: any; messages: any; constructor(private a

请帮助,我有一个消息服务,它从数据库获取我的消息,并将它们显示在用户配置文件页面上。创建或删除邮件时,我只能在刷新浏览器时看到更改

在我应用过滤器按Id排序之前,页面会动态更新,为什么过滤器会取消绑定我的消息数组

配置文件组件:

    export class ProfileComponent implements OnInit {

    user: User;
    userId: any;
    messages: any;  

    constructor(private authService: AuthService, private messageService: MessageService) {}

    ngOnInit() {
      this.authService.getUser().subscribe(
        (user: User) => {
        this.user = user;
        this.userId = user.userId;
        }
      );
       this.messageService.getMessages()
      .subscribe(
          (messages: Message[]) => {
            this.messages = messages.filter(x => x.userId == this.userId);
            //this.messages = messages; //this works perfect, but shows all users messages
          }
      );
    }
}

似乎您的第一个API调用(
getUser
method)花费了更多的时间,它在
getMessages
方法之后完成。 你应该设法让他们依赖。您可以使用
toPromise
方法将
getUser
Observable转换为promise

ngOnInit() {
  this.authService.getUser()
  .toPromise()
  .then(
     (user: User) => {
        this.user = user;
        this.userId = user.userId;
     }
  )
  .then(() => {
      this.messageService.getMessages()
      .subscribe(
         (messages: Message[]) => {
            this.messages = messages.filter(x => x.userId == this.userId);
         }
      );
  });
}

将代码更改为:

messages: Array<any> = [];  
this.authService.getUser().subscribe(
        (user: User) => {
        this.user = user;
        this.userId = user.userId;
        this.messageService.getMessages()
              .subscribe(
                  (messages: Message[]) => {
                    this.messages = messages.filter(x => x.userId === this.userId);
                    //this.messages = messages; //this works perfect, but shows all users messages
                  }
              );
        }
      );
消息:数组=[];
此.authService.getUser().subscribe(
(用户:用户)=>{
this.user=用户;
this.userId=user.userId;
this.messageService.getMessages()
.订阅(
(messages:Message[])=>{
this.messages=messages.filter(x=>x.userId==this.userId);
//this.messages=messages;//这非常有效,但会显示所有用户的消息
}
);
}
);

您只需要嵌套其他可观察对象,因此在第一个完成调用后将调用它。

这并没有解决它。我的服务返回一个要显示的消息数组,当我打开过滤器对消息进行排序时,它似乎弄乱了在原始数组上工作的前端代码。我太迷路了,唯一的解决办法就是调用重新加载来浏览:(请使用此代码再次检查此项,好吗?代码有效,但页面在我刷新之前不会显示结果。在angular 2中强制页面刷新是否是错误的编码?不,这不是错误的编码,只需等待一段时间,它将自动显示所有结果,这就是它的工作方式。由于这些错误,页面显示信息是否常见iming问题?是否有办法确保在代码执行之前从数据库加载数据?谢谢,但在刷新页面之前,我仍然看不到创建和删除的更改。在设置过滤器之前,我可以完美地处理消息数组。我真的希望有人能帮我。我不想知道什么为什么我的视图不会自己更新,因为它订阅了我正在添加和删除的相同消息