【发布时间】:2021-07-18 17:11:40
【问题描述】:
当客户端代码中的console.log 时,此 Meteor 代码未能返回 Vehicles 集合中的文档数。请帮助找出我出错的问题。谢谢
//imports/api/vehicles.js
import {Mongo} from 'meteor/mongo';
export const Vehicles = new Mongo.Collection('vehicles');
///////////////////////////////////////////
//server/publish.js
import {Vehicles} from '../imports/api/vehicles.js'
Meteor.publish('vehicles', function(){
return Vehicles.find({})
})
///////////////////////////////////////////
//server/main.js
import { Vehicles } from '../imports/api/vehicles.js';
//so I added this tying to fix the problem for no avail
Meteor.startup(() => {
Meteor.publish('vehicles', function () {
return Vehicles.find();
});
});
///////////////////////////////////////////
//client/main.js
import {Vehicles} from '../imports/api/vehicles.js'
Meteor.startup(function(){
Meteor.subscribe('vehicles');
console.log('subscribed') //<<<<<< prints "subscribed"
let rec = Vehicles.find({}).count()
console.log(rec) //<<<<<< prints "0"
})
///////////////////////////////////////////
Blaze 集合 findOne 不返回任何文档
阅读第一个回复后,关于 Blaze 会自动执行,而我不必这样做 Meteor.startup Meteor.subscribe.onReady... 请注意下面的 blaze 模板助手给出了 undefined。
//client/main.js
Tracker.autorun(() => {
Meteor.subscribe('vehicles', {plate: Session.get('plate')});
});
Template.vehicle.helpers({
'vehicle' : function(){
let vehicle = Vehicles.findOne({'plate':Session.get('plate')})
console.log(vehicle) //prints undefined
}
})
Template.vehicle.events({
'keyup #plate'(e, inst){
let str = e.currentTarget.value
Session.set('plate', str)
console.log(str) // prints the value OK
}
})
【问题讨论】:
-
Meteor.subscribe的 onError 键被触发,其参数打印在浏览器控制台中,显示“未找到订阅‘车辆’[404]”。但它存在于单独的提示符meteor mongo> 显示集合> 车辆