无法捕捉流星.当流星方法的服务器端抛出错误时,客户端出现错误



我有一个流星调用和流星方法。当我抛出Meteor错误时,它不会被捕捉到流星调用的错误中,而是作为的响应

这是我的流星方法

Meteor.call("updateFtIndex", id, $(`#idx-${id}`).val(), function (error, result) {
if (error) {
console.log('error: ', error.reason);
}
else {
console.log('result', result);
}
})

这是我的流星法

Meteor.methods({
updateFtIndex: function (_id, index) {
try {
let isIndexExists = Meteor.users.findOne({_id, "profile.featuredProviderIndex": index }, {fields: 
{"profile.featuredProviderIndex": 1}});
if(isIndexExists){
console.log('isIndexExists: ', isIndexExists);
throw new Meteor.Error( 400, "Sorted index already exist !!!");
}
else {
console.log("else");
return Meteor.users.update({_id}, { $set: { "profile.featuredProviderIndex": index } });
}
} catch (error) {
return error
}
}
})

我得到了流星呼叫结果中的抛出错误有人能告诉我代码出了什么问题吗?

您正在接球,您的接球正在返回错误,但将错误返回到前面的更好方法是将其更改为:

try {
let isIndexExists = Meteor.users.findOne({_id, "profile.featuredProviderIndex": index }, {fields:  {"profile.featuredProviderIndex": 1}});
if(isIndexExists){
console.log('isIndexExists: ', isIndexExists);
throw new Meteor.Error( 400, "Sorted index already exist !!!");
}
else {
console.log("else");
return Meteor.users.update({_id}, { $set: { "profile.featuredProviderIndex": index } });
}
} catch (e) {
throw new Meteor.Error(400, e.toString());
}

您返回的是错误,而不是从catch块中抛出错误。

最新更新