gpt4 book ai didi

javascript - 为什么在使用非空对象调用 Meteor.method 时服务器得到一个空对象?

转载 作者:行者123 更新时间:2023-11-29 19:15:42 24 4
gpt4 key购买 nike

如果可能的话,如何使用对象作为参数调用 Meteor 方法?

如果有帮助,这是我正在努力解决的问题

我有方法:

'user.some.update.position'(coords) {
console.log('method: ', 'user.some.update.position');
console.log('this.userid: ', this.userId);
console.log('coords: ', coords);

check(coords, Object);
check(coords.latitude, Number);
check(coords.longitude, Number);
check(coords.accuracy, Number);

if (!this.userId)
throw new Meteor.Error('Not logged in', 'You are not logged in, please log in');

Meteor.users.update({
_id: this.userId
}, {
$set: {
coords,
lastUpdated: new Date()
}
});
return coords;
}

我想像这样从客户端调用:

> var coords = Geolocation.currentLocation().coords
undefined
> coords
Coordinates {latitude: 58.2441766, longitude: 8.376727899999999, altitude: null, accuracy: 25, altitudeAccuracy: null…}
> Meteor.call('user.some.update.position', coords, function(err, res) {if(err) console.log('err: ', err); if(res) console.log('res: ', res);})
undefined
VM7572:2 err: errorClass {error: 400, reason: "Match failed", details: undefined, message: "Match failed [400]", errorType: "Meteor.Error"}

但是当我这样做时,服务器会提示 coords 是一个空对象,如下所示:

method:  user.some.update.position
I20160220-15:49:34.226(1)? this.userid: nHqj3zaSWExRmqBZq
I20160220-15:49:34.226(1)? currentLocation: {}
I20160220-15:49:34.227(1)? Exception while invoking method 'user.some.update.position' Error: Match error: Expected number, got undefined
I20160220-15:49:34.227(1)? at Object.check (packages/check/match.js:33:1)
I20160220-15:49:34.228(1)? at [object Object]._meteorMeteor.Meteor.methods.user.some.update.position (server/methods/drivers.js:36:5)
I20160220-15:49:34.228(1)? at packages/check/match.js:103:1
I20160220-15:49:34.228(1)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160220-15:49:34.228(1)? at Object.Match._failIfArgumentsAreNotAllChecked (packages/check/match.js:102:1)
I20160220-15:49:34.228(1)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1695:18)
I20160220-15:49:34.228(1)? at packages/ddp-server/livedata_server.js:708:19
I20160220-15:49:34.228(1)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160220-15:49:34.228(1)? at packages/ddp-server/livedata_server.js:706:40
I20160220-15:49:34.229(1)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160220-15:49:34.229(1)? Sanitized and reported to the client as: Match failed [400]

客户提示:

err:  errorClass {error: 400, reason: "Match failed", details: undefined, message: "Match failed [400]", errorType: "Meteor.Error"}

编辑:我正在使用 ES6 和对象解构。

最佳答案

从您的错误消息中可以清楚地看出您的对象中没有任何内容。请查看错误消息的第 4 行,currentLocation 不包含任何属性。发送一个有效的对象将解决问题。

关于javascript - 为什么在使用非空对象调用 Meteor.method 时服务器得到一个空对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35524933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com