I have this code on the client:
var Checklist = {
title: this.title,
belongs_to: this.belongs_to,
type: this.type,
items: this.items
};
Meteor.call(
'create_checklist',
Checklist,
function(error,result){
console.log('error',error,'result',result);
// if(!error) {
// Router.go('/checklist/'+response);
// }
}
);
And this on the server:
create_checklist: function(Checklist) {
Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}, function(error,id){
console.log(error,id);
if(id) {
return id;
} else {
return error;
}
}
);
},
The Meteor.call passes the information successfully to the server, as the checklist is created. I can see in the server console the ID of the new checklist. However, the client only sees undefined
for both error and result.
I have this code on the client:
var Checklist = {
title: this.title,
belongs_to: this.belongs_to,
type: this.type,
items: this.items
};
Meteor.call(
'create_checklist',
Checklist,
function(error,result){
console.log('error',error,'result',result);
// if(!error) {
// Router.go('/checklist/'+response);
// }
}
);
And this on the server:
create_checklist: function(Checklist) {
Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}, function(error,id){
console.log(error,id);
if(id) {
return id;
} else {
return error;
}
}
);
},
The Meteor.call passes the information successfully to the server, as the checklist is created. I can see in the server console the ID of the new checklist. However, the client only sees undefined
for both error and result.
3 Answers
Reset to default 5You don't return result in your server method. You can't return values from callback. Return just result of Checklists.insert:
create_checklist: function(Checklist) {
return Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}, function(error,id){
console.log(error,id);
if(id) {
return id;
} else {
return error;
}
}
);
},
According to Meteor docs, insert method returns ID of inserted document.
On the server, if you don't provide a callback, then insert blocks until the database acknowledges the write, or throws an exception if something went wrong.
You don't need to return anything, change the meteor method to this.
create_checklist: function(Checklist) {
Checklists.insert(
{
title: Checklist.title,
belongs_to: Checklist.belongs_to,
type: Checklist.type,
items: Checklist.items
}
);
}
The meteor.call callback
know how to handle the server responde thats why you are using error result
, if something gets wrong on the method the server will throw an error and the meteor call will fail.
Simplified to the bare minimum:
create_checklist: function(Checklist) {
return Checklists.insert(Checklist);
}
Your client code should see the _id
of the inserted document in result
I do have to ask though, why? You should be able to do var id = Checklists.insert(Checklist)
on the client if the collection is published and let Meteor handle the synchronization to the server. Is Checklists
not published to the client?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744980925a4604435.html
评论列表(0条)