运行Files.find().fetch()
时,我的Chrome控制台中只显示了5个灯具。正如您所看到的,我还有更多我想要添加到我的收藏中的灯具。第一个文件位于/ lib中,名为files.js
,如下所示:
Files = new Mongo.Collection('files');
Meteor.methods({
deleteFile: function(id) {
// check to see if user is actually the owner of the file
var file = Files.findOne(id);
if (Meteor.userId() != file.owner) {
// user could've sent any data, but we want the one in the database,
// so we find do Files.findOne(id).owner instead of data.owner
throw new Meteor.Error('Not Authorized', 'User is not the right owner?');
}
Files.remove({_id: id});
},
insertFile: function(data) {
// user must be logged in
if (!Meteor.userId()) {
throw new Meteor.Error('Not Authorized', 'User is not logged in!');
}
// set owner
data.owner = Meteor.userId();
// insert into the db collection
Files.insert(data);
},
updateFile: function(id, data) {
// user must be logged in
if (!Meteor.userId()) {
throw new Meteor.Error('Not Authorized', 'User is not logged in!');
}
// check to see if user is actually the owner of the file
var file = Files.findOne(id);
if (Meteor.userId() != file.owner) {
// user could've sent any data, but we want the one in the database,
// so we find do Files.findOne(id).owner instead of data.owner
throw new Meteor.Error('Not Authorized', 'User is not the right owner?');
}
// ensure data belongs to current user, client can't change the user
data.owner = Meteor.userId();
// update the db collection
Files.update(id, data);
}
});
第二个文件位于/ server中,名为fixtures.js
,如下所示:
Meteor.startup(function() {
// code to run on server at startup
// count recipes entry
var num = Files.find().count();
if (num === 0) {
var fixtures = [
{
username: 'FakeUser-1',
firstName: 'John1',
lastName: 'Smith',
email: 'k@opt.net',
phone: '555-555-5555',
streetAdd: '8 Milk Street',
town: 'Compton',
state: 'NY',
econtact: {
eName: 'Jane Smith',
phone: '555-555-5555'
},
prefs: 'deaf',
admin: false
},
{
username: 'FakeUser-2',
firstName: 'John2',
lastName: 'Smith',
email: 'k@opt.net',
phone: '555-555-5555',
streetAdd: '8 Milk Street',
town: 'Compton',
state: 'NY',
econtact: {
eName: 'Jane Smith',
phone: '555-555-5555'
},
prefs: 'deaf',
admin: false
},
{
username: 'FakeUser-3',
firstName: 'John3',
lastName: 'Smith',
email: 'k@opt.net',
phone: '555-555-5555',
streetAdd: '8 Milk Street',
town: 'Compton',
state: 'NY',
econtact: {
eName: 'Jane Smith',
phone: '555-555-5555'
},
prefs: 'deaf',
admin: false
},
{
username: 'FakeUser-4',
firstName: 'John4',
lastName: 'Smith',
email: 'k@opt.net',
phone: '555-555-5555',
streetAdd: '8 Milk Street',
town: 'Compton',
state: 'NY',
econtact: {
eName: 'Jane Smith',
phone: '555-555-5555'
},
prefs: 'deaf',
admin: true
},
{
owner: 'FakeUser-1',
fileName: 'my_FakeFileAA.py',
ancestor: 'master-fakeFileA.py',
content: '',
type: 'python',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-1',
fileName: 'FakeFileAB.py',
ancestor: 'master-fakeFileA.py',
content: '',
type: 'python',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-1',
fileName: 'myFakeFileBA.py',
ancestor: 'master-fakeFileB.py',
content: '',
type: 'python',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-1',
fileName: 'myFakeFileBB.py',
ancestor: 'master-fakeFileB.py',
content: '',
type: 'python',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-1',
fileName: 'default-1A.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-1',
fileName: 'default-1B.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-2',
fileName: 'default-2A.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-3',
fileName: 'default-3A.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-3',
fileName: 'default-3B.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-3',
fileName: 'default-3C.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-3',
fileName: 'default-3D.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-3',
fileName: 'default-3E.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: true
},
{
owner: 'FakeUser-4',
fileName: 'default.web',
ancestor: 'default.web',
content: ['', '', ''],
type: 'web',
creationDate: new Date(),
modifiedDate: new Date(),
private: false
}
];
fixtures.forEach(function(element) {
Files.insert(element);
});
}
});
特别是,正在插入的仅有5个文档是上面列出的前4个文档和最后一个(可能这有帮助)。我正在使用Meteor.js版本1.5.1。
修订:
另外,当我打开meteor mongo
> use files
> db.files.find().count()
我得到0.我真的不知所措。
提前致谢!
答案 0 :(得分:0)
您的出版物,我的代码我看不到,是错误的。您可能正在使用Meteor.publish('files', function() {return Files.find({private: { $ne:true}});})
,这显然只会返回5个非私人文件。
如果您在客户端上查询计数,它只会显示客户端看到的内容。