Borsh 序列化在 React 应用程序中失败
Borsh Serialization fails in React Application
此脚本无法反序列化我的对象。错误是 TypeError: this.buf.readUInt32LE is not a function
TypeError: this.buf.readUInt32LE is not a function
at BinaryReader.readU32 (index.js:165)
at BinaryReader.propertyDescriptor.value (index.js:136)
at BinaryReader.readString (index.js:194)
at BinaryReader.propertyDescriptor.value (index.js:136)
at deserializeField (index.js:341)
at deserializeStruct (index.js:385)
at Object.deserialize (index.js:403)
at hello (App.js:52)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
at executeDispatch (react-dom.development.js:8243)
at processDispatchQueueItemsInOrder (react-dom.development.js:8275)
at processDispatchQueue (react-dom.development.js:8288)
at dispatchEventsForPlugins (react-dom.development.js:8299)
at react-dom.development.js:8508
at batchedEventUpdates (react-dom.development.js:22396)
at batchedEventUpdates (react-dom.development.js:3745)
at dispatchEventForPluginEventSystem (react-dom.development.js:8507)
at attemptToDispatchEvent (react-dom.development.js:6005)
at dispatchEvent (react-dom.development.js:5924)
at unstable_runWithPriority (scheduler.development.js:468)
at runWithPriority (react-dom.development.js:11276)
at discreteUpdates (react-dom.development.js:22413)
at discreteUpdates (react-dom.development.js:3756)
at dispatchDiscreteEvent (react-dom.development.js:5889)
这在 CodesandBox 上工作正常,但在本地机器上失败。
const borsh = require("borsh");
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
return (this[key] = properties[key]);
});
}
}
class Task extends Assignable { }
export default function App() {
const example = {
id: "12",
assignee: "Sai",
project_id: "ABX",
name: "Hello",
description: "Sample task Example",
status: "NOT DONE"
};
const schema = new Map([
[
Task,
{
kind: "struct",
fields: [
["id", "string"],
["name", "string"],
["description", "string"],
["assignee", "string"],
["project_id", "string"],
["status", "string"]
]
}
]
]);
function hello() {
const task = new Task({
id: example.id,
name: example.name,
description: example.description,
assignee: example.assignee,
project_id: example.project_id,
status: example.status
});
console.log(task);
const buf = borsh.serialize(schema, task);
console.log(buf);
try {
const newValue = borsh.deserialize(schema, Task, buf);
console.log(newValue);
} catch (error) {
console.log(error);
}
}
return (
<div className="APPLE">
<button onClick={hello}>Create</button>
</div>
);
}
我查看了 codesandbox 页面中的依赖项,它全局导入了 buffer 包。我尝试在本地用 require('buffer/')
做同样的事情,但没有成功。但是,它可以将其用作 const Buffer = require('buffer/').Buffer
并调用方法 Buffer.from
从序列化数据中获取缓冲区。
const Buffer = require('buffer/').Buffer
...
const buf = Buffer.from(borsh.serialize(schema, task));
此脚本无法反序列化我的对象。错误是 TypeError: this.buf.readUInt32LE is not a function
TypeError: this.buf.readUInt32LE is not a function
at BinaryReader.readU32 (index.js:165)
at BinaryReader.propertyDescriptor.value (index.js:136)
at BinaryReader.readString (index.js:194)
at BinaryReader.propertyDescriptor.value (index.js:136)
at deserializeField (index.js:341)
at deserializeStruct (index.js:385)
at Object.deserialize (index.js:403)
at hello (App.js:52)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
at invokeGuardedCallback (react-dom.development.js:4056)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4070)
at executeDispatch (react-dom.development.js:8243)
at processDispatchQueueItemsInOrder (react-dom.development.js:8275)
at processDispatchQueue (react-dom.development.js:8288)
at dispatchEventsForPlugins (react-dom.development.js:8299)
at react-dom.development.js:8508
at batchedEventUpdates (react-dom.development.js:22396)
at batchedEventUpdates (react-dom.development.js:3745)
at dispatchEventForPluginEventSystem (react-dom.development.js:8507)
at attemptToDispatchEvent (react-dom.development.js:6005)
at dispatchEvent (react-dom.development.js:5924)
at unstable_runWithPriority (scheduler.development.js:468)
at runWithPriority (react-dom.development.js:11276)
at discreteUpdates (react-dom.development.js:22413)
at discreteUpdates (react-dom.development.js:3756)
at dispatchDiscreteEvent (react-dom.development.js:5889)
这在 CodesandBox 上工作正常,但在本地机器上失败。
const borsh = require("borsh");
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
return (this[key] = properties[key]);
});
}
}
class Task extends Assignable { }
export default function App() {
const example = {
id: "12",
assignee: "Sai",
project_id: "ABX",
name: "Hello",
description: "Sample task Example",
status: "NOT DONE"
};
const schema = new Map([
[
Task,
{
kind: "struct",
fields: [
["id", "string"],
["name", "string"],
["description", "string"],
["assignee", "string"],
["project_id", "string"],
["status", "string"]
]
}
]
]);
function hello() {
const task = new Task({
id: example.id,
name: example.name,
description: example.description,
assignee: example.assignee,
project_id: example.project_id,
status: example.status
});
console.log(task);
const buf = borsh.serialize(schema, task);
console.log(buf);
try {
const newValue = borsh.deserialize(schema, Task, buf);
console.log(newValue);
} catch (error) {
console.log(error);
}
}
return (
<div className="APPLE">
<button onClick={hello}>Create</button>
</div>
);
}
我查看了 codesandbox 页面中的依赖项,它全局导入了 buffer 包。我尝试在本地用 require('buffer/')
做同样的事情,但没有成功。但是,它可以将其用作 const Buffer = require('buffer/').Buffer
并调用方法 Buffer.from
从序列化数据中获取缓冲区。
const Buffer = require('buffer/').Buffer
...
const buf = Buffer.from(borsh.serialize(schema, task));