2017-02-23 44 views
0

我在我的應用程序中有一個功能來顯示客戶列表,然後編輯所有客戶的頁面。我得到的錯誤,當用戶重定向到編輯頁面 「預期模板或空,發現:未定義」預期的模板或null,找到:undefined

下面是我參考 Route.js

Router.route('/user/:userId', function() { 
    this.render('customeredit'); 
    this.layout('mainNav'); 
}); 

HTML

<template name="customeredit"> 
    {{#customerData}} 
    {{fname}} 
    {{/customerData}} 
</template> 

JS代碼

import { Template } from 'meteor/templating'; 
import { ReactiveVar } from 'meteor/reactive-var'; 

import '../templates/editUser.html'; 

import { Customers } from '../../imports/api/customers.js'; 

Template.customeredit.helpers({ 
    customerData: function() { 
     var customerId = 'NpWkSWRpkQJKsNecG'; 
     console.log(customerId); 
     console.log(Customers.findOne({_id: customerId})); 
    } 
}); 

也請告訴我們如何從url中獲取用戶ID在m y以上的JS文件,因爲我生成的URL是:http://localhost:3000/user/NpWkSWRpkQJKsNecG,我需要在助手中獲取「NpWkSWRpkQJKsNecG」。

回答

0

希望這可以解決您的問題並回答您的問題。

  1. 爲什麼您收到「預期的模板或null,發現:undefined」錯誤?

您是否提供了問題中所有頁面的代碼?如果是這樣,那麼我懷疑這個問題出現在你的模板定義中,你使用的是自定義塊助手(例如{{#customerData}})。

您是否定義了此塊幫助程序?我想知道你是否只是試圖使用customerData模板助手。如果是這樣的話,那麼你的模板定義應該是這樣的:

<template name="customeredit"> 
    {{customerData}} 
</template> 

模板幫手調用只是簡單包裹在{{ }}

  1. 如何從url中訪問用戶ID參數。

使用鐵路路由器,您可以使用this.params訪問路由參數。您通常會先查看數據,然後將其傳遞給模板。例如:

Router.route('/user/:userId', function() { 
    var user = Customers.findOne({_id: this.params.userId}); 
    this.render('customeredit', {data: user}); 
    this.layout('mainNav'); 
});