At present, the development with meteor and ng2 has encountered many problems. I think most of it is because of asynchrony. When meteor and the third-party interface call each other, the code is not executed in synchronous order, causing problems. The details are as follows
import {Component, View} from 'angular2/core';
import {Statuslist} from 'collections/Statuslist';
import {RouterLink,RouteParams,Router} from 'angular2/router';
@Component({
selector: 'status-list'
})
@View({
templateUrl: '/client/tpl/main.html',
directives: [ RouterLink]
})
export class Main {
chlidren: Mongo.Cursor<Object>;
constructor(params: RouteParams,private _router: Router) {
var sid = params.get('sid'); //Obtain site id according to parameter sid
Meteor.subscribe('statuslist');
if(! Sid){// if sid is 0
var tmp = Statuslist.findOne();
// console.log(tmp); This is undefined. I think it may be asynchronous
sid = tmp.sid;
bracket
this.chlidren = Statuslist.findOne({fid:sid});
bracket
/client/tpl/main.html
<div>{{chlidren.sname}}</div>
The logic of my business is to display a sub-site under the site according to the parameter sid.
When sid is 0, I will look for a site casually to show the subsites.
I found that when sid is 0, it may be because nodejs is asynchronous. last sentence
this.chlidren = Statuslist.findOne({fid:sid});
Inside’s sid has not been reassigned, it is still 0, and tmp output is undefined.
At the same time, html will also report an error, saying undefined does not have sname
How should I deal with this situation? Let chlidren get the correct data?
Try putting that code in autorun inside?