gpt4 book ai didi

javascript - 通过 JSONAPI-Serializer 进行反序列化时遇到奇怪的问题

转载 作者:太空宇宙 更新时间:2023-11-04 00:26:04 24 4
gpt4 key购买 nike

如果我喜欢这个

var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;

var UserDeserialize = new JSONAPIDeserializer('id');
UserDeserialize.deserialize({ data: [ { type: 'ids', id: '1' }, { type: 'ids', id: '2' } ] })
.then(a => console.log(a))

我能够获得所需的反序列化输出

[ { id: '1' }, { id: '2' } ]

但是如果我创建一个序列化数据变量并尝试像这样序列化相同的变量

var JSONAPIDeserializer = require('jsonapi-serializer').Deserializer;

var test = "{ data: [ { type: 'ids', id: '1' }, { type: 'ids', id: '2' } ] }"
var UserDeserialize = new JSONAPIDeserializer('id');
UserDeserialize.deserialize(test)
.then(a => console.log(a))

我收到这样的错误:-

/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer-utils.js:70
var dest = keyForAttribute(from.attributes || {});
^

TypeError: Cannot read property 'attributes' of undefined
at extractAttributes (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer-utils.js:70:36)
at module.exports.perform (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer-utils.js:123:13)
at resource (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer.js:26:10)
at module.exports.deserialize (/Users/acid/node_js/node_modules/jsonapi-serializer/lib/deserializer.js:39:14)
at Object.<anonymous> (/Users/acid/node_js/localhost/velekaam/serial.js:13:17)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:425:7)
at startup (bootstrap_node.js:146:9)
at bootstrap_node.js:540:3

我该如何纠正这个问题?

最佳答案

替换...

var test = "{ data: [ { type: 'ids', id: '1' }, { type: 'ids', id: '2' } ] }"
UserDeserialize.deserialize(test)

...与:

var test = '{"data":[{"type":"ids","id":"1"},{"type":"ids","id":"2"}]}'
UserDeserialize.deserialize(JSON.parse(test))

...或者,与:

var test = { data: [ { type: 'ids', id: '1' }, { type: 'ids', id: '2' } ] }
UserDeserialize.deserialize(test)

关于javascript - 通过 JSONAPI-Serializer 进行反序列化时遇到奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42747668/

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