【发布时间】:2014-01-29 06:08:16
【问题描述】:
我有以下 thrift 文件:
union D{ 1: string s; }
struct B{ 1: required D d; }
struct C{ 1: required D d; }
union A{ 1: B b; 2: C c; }
service Test { void store(1: A a) }
我有以下 JSON 对象,它是通过解析字符串获得的。
var data_json = {
'b': {
'd': {
's': "hello"
}
}
};
我正在尝试在 Nodejs 中编写一个节俭客户端,它以 data_json 作为参数调用 store 方法,但是这样做时出现以下错误:
/home/aakash/Documents/thrift0/gen-nodejs/test_types.js:225
this.b.write(output);
^
TypeError: Object #<Object> has no method 'write'
at Object.A.write (/home/aakash/Documents/thrift0/gen-nodejs/test_types.js:225:12)
at Object.Test_store_args.write (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:57:12)
at Object.TestClient.send_store (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:113:8)
at Object.TestClient.store (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:105:8)
at Object.<anonymous> (/home/aakash/Documents/thrift0/client.js:40:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
但是,当我将以下对象作为参数传递时,它可以正常工作:
var data_thrift = new ttypes.A({
'b': new ttypes.B({
'd': new ttypes.D({
's': "hello"
})
})
});
有没有办法将data_json 直接传递给store,或者将data_json 转换为data_thrift 的方法?
【问题讨论】:
标签: javascript json node.js thrift