我正在使用JavaScript和neo4j-driver
保存一个 node 和一些与图形的关系.一切都按预期进行,但我希望读取写入结果,并在之后以特定格式输出,这是我遇到的问题.目前,我只返回了查询中的所有变量,但我希望输出采用以下特定格式:
{
"name": p.name
"colors: [
{
hex: c.hex
}
{
hex: c.hex
}
]
}
这是我的代码,非常感谢您的帮助:
export const savePallete = async (palette: Palette) => {
if (!!palette.name === false) {
throw Error("Palette name required!");
}
const writeQuery = `MERGE (p:Palette {name: $name})
ON CREATE SET p.name = $name
FOREACH (color IN $colors |
MERGE (c:Color {hex: color.hex})
ON CREATE SET c.hex = color.hex
CREATE (p)-[i:INCLUDES]->(c))
`;
await session.writeTransaction((tx) =>
tx.run(writeQuery, { name: palette.name, colors: palette.colors })
);
const readQuery = `
MATCH (p:Palette)
WHERE p.name = $name
MATCH (p)-[:INCLUDES]->(c:Color)
RETURN *
`;
const readResult = await session.readTransaction((tx) =>
tx.run(readQuery, { name: palette.name })
);
return readResult;
};
例如,如果我用这个对象作为参数调用savePallete
,我希望得到相同的结果:
{
"name": "First test palette",
"colors": [
{
"hex": "#123123"
},
{
"hex": "#ffffff"
},
{
"hex": "#000000"
}
]
}