为什么在使用非空对象调用 Meteor.method 时服务器会得到一个空对象? [英] Why is the server getting an empty object when calling Meteor.method with a non-empty object?

查看:50
本文介绍了为什么在使用非空对象调用 Meteor.method 时服务器会得到一个空对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I call a Meteor method with a object as an argument, if this is at all possible?

如果有帮助,这就是我正在努力的地方

Here is what I'm struggling with if it helps

我有方法:

'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;
  }

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

Which I want to call from the client like this:

> 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 是一个像这样的空对象:

But when I do that the server complains that coords is a empty object like this:

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 和对象解构.

I am using ES6 and object destructuring.

推荐答案

您从地理定位请求中获得的对象实现了 坐标 界面.你不能假设其他任何事情.

The object that you are getting from the geolocation request implements the Coordinates interface. You cannot assume anything else about it.

在您的情况下,该对象可能无法通过 EJSON 序列化,因此不能按原样用作 Meteor 方法调用参数.

In your case, the object is likely not serializable via EJSON, and therefore cannot be used as a Meteor method call argument as it is.

方法调用例程调用 EJSON.clone() 并给定 Coordinates 对象,它返回一个空对象.

The method call routine calls EJSON.clone() and given the Coordinates object, it returns an empty object.

> EJSON.clone(coordinates)
Object {}

在 Chrome 的实现中,我假设属性在原型链的更深处被拥有".EJSON 在克隆时依赖于 underscore 的 _.keys 函数,该函数依次列出对象自身的属性.

In Chrome's implementation, I assume that the properties are "owned" deeper in the prototype chain. EJSON relies on underscore's _.keys function when cloning, which in turn lists the object's own properties.

> coordinates.hasOwnProperty("latitude")
false
> "latitude" in coordinates
true

由于 为它创建自定义 EJSON 类型似乎不切实际,您可以按照@ChristianFritz 的建议进行操作或使用下划线的 _.pick.

Since creating a custom EJSON type for it seems unrealistic, you can do as @ChristianFritz suggested or use underscore's _.pick.

> _.pick(coordinates, ['latitude', 'longitude', 'accuracy'])
Object {latitude: 12.1212, longitude: 34.3434, accuracy: 50}

这篇关于为什么在使用非空对象调用 Meteor.method 时服务器会得到一个空对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆