开发智联笔记项目时所遇问题(6)
问题:点击新建脑图,有命名,但是没有节点。
问题在于创建脑图时的数据结构格式不正确。jsMind 需要特定的节点格式。
修改 index.html 中的 createMap() 函数
/* 新建 */
function newMap() {
document.getElementById('newModal').style.display = 'block';
}
async function createMap() {
const name = document.getElementById('mName').value.trim();
const topic = document.getElementById('mTopic').value.trim();
if (!name || !topic) return alert('请填写完整');
// 修正:使用 jsmind 正确的数据格式
const data = {
"id": "root",
"topic": topic,
"direction": "right",
"expanded": true,
"children": []
};
try {
const res = await fetch('/api/mindmaps', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name, data}),
credentials: 'include'
});
if (!res.ok) {
const error = await res.text();
throw new Error(error || '创建失败');
}
const m = await res.json();
mapList.push(m);
renderList();
closeModal('newModal');
openMap(m.id);
} catch (error) {
console.error('Create map error:', error);
alert('创建失败: ' + error.message);
}
}
同时修复 openMap() 函数
/* 打开 / 保存 / 删除 */
async function openMap(id) {
try {
currentId = id;
renderList();
const res = await fetch(`/api/mindmaps/${id}`, {credentials: 'include'});
if (!res.ok) throw new Error('加载脑图失败');
const m = await res.json();
console.log('Loaded mind map data:', m);
// 确保数据格式正确
let mindData = m.data;
if (typeof mindData === 'string') {
try {
mindData = JSON.parse(mindData);
} catch (e) {
console.error('Parse mind data error:', e);
}

浙公网安备 33010602011771号