我正在尝试构建我的Firebase数据库,因此所有用户的练习都属于同一个postkey,因为我正在为每个练习做一个新的推送请求,他们目前没有正确的结构,如此处所示:http://imgur.com/a/Wz93o
我的Firebase代码是:
function writeNewWorkout() {
// A post entry.
console.log(payload);
var uid = firebase.auth().currentUser.uid;
// Get a key for a new Post
var newPostKey = firebase.database().ref().child('Workout').push().key;
// Write the new post's data
var updates = {};
updates[uid + '/Workout/' + newPostKey + "/" + exercise] = payload; // Add UID?
return firebase.database().ref().update(updates);
}
和收集用户条目的提交功能(动态字段输入):
$('.submitExercise').click(function () {
var reps = [];
$.each($('.repsClass'), function () {
reps.push($(this).val());
});
if (reps.length == 0) {
reps = "none";
}
var weight = [];
$.each($('.weightClass'), function () {
weight.push($(this).val());
});
if (weight.length == 0) {
weight = "none";
}
exercise = document.getElementById("autocomplete-input").value;
alert(exercise);
alert(reps);
alert(weight);
for (var i = 0; i <= reps.length - 1; i++)
{
payload["exercise"] = exercise,
payload["reps" + i] = reps[i],
payload["weight" + i] = weight[i];
}
writeNewWorkout();
});
有人可以告诉我应该做出哪些改变吗?
我想我需要一个for循环和一个更新方法,我只是不确定如何构建它。
编辑:
我想出了以下内容,而不是使用postkey,我使用的是日期变量(因为它是可预测的),如下所示:http://imgur.com/a/0IgPc
这是否可以接受?
JSON结构:
{
"ZwYo0nB92oODcjjxJ0Eqeo4l3Ny1" : {
"BodyStats" : {
"-KbDFBzjRIVtaFWN325R" : {
"arms" : "5",
"bodyfat" : "2",
"calves" : "3",
"chest" : "4",
"date" : "23/1/2017",
"forearms" : "5",
"neck" : "6",
"shoulders" : "7",
"thighs" : "8",
"waist" : "9",
"weight" : "10"
},
"-KbDHVZlyRHZnp7iwC80" : {
"arms" : "5",
"bodyfat" : "4",
"calves" : "884",
"chest" : "65",
"date" : "22/1/2017",
"forearms" : "56",
"neck" : "5",
"shoulders" : "14",
"thighs" : "42",
"waist" : "66",
"weight" : "44"
},
"-KbDISlDA2c9HDvySxyY" : {
"arms" : "",
"bodyfat" : "",
"calves" : "2",
"chest" : "",
"date" : "1/1/2017",
"forearms" : "",
"neck" : "",
"shoulders" : "",
"thighs" : "",
"waist" : "",
"weight" : ""
}
},
"Journal" : {
"-KbGSz7AqoRMId5S8pvV" : {
"date" : "25/1/2017",
"entry" : "",
"title" : ""
},
"-KbGT3lFEMeg3TjeaQpY" : {
"date" : "18/1/2017",
"entry" : "",
"title" : ""
},
"-KbGTBRO5FEwJXi3Ea0f" : {
"date" : "13/1/2017",
"entry" : "",
"title" : ""
},
"-KbGTaLDpL6qrdNQn4am" : {
"date" : "24/1/2017",
"entry" : "Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. Hello this is a test. ",
"title" : "Test"
}
},
"Somatics" : {
"-KbDEj4lyV1YeDIiiHxa" : {
"comments" : "c",
"date" : "23/1/2017",
"energy" : "Mostly Energetic",
"mood" : "Mostly Euphoric",
"nutrition" : "Today was a good day, energetic, did a bunch, this is a test. Hellow how are you?",
"sleep" : "Above Average"
},
"-KbDG2QT6R5fShe-y88_" : {
"comments" : "",
"date" : "24/1/2017",
"energy" : "Slightly Energetic",
"mood" : "Euphoric",
"nutrition" : "",
"sleep" : "Below Average"
},
"-KbDImS8hW_mg7PLNh4b" : {
"comments" : "",
"date" : "1/1/2017",
"energy" : "Mostly Energetic",
"mood" : "",
"nutrition" : "",
"sleep" : ""
},
"-KbDLP1owhsUzu2F_Vzj" : {
"comments" : "",
"date" : "25/1/2017",
"energy" : "",
"mood" : "Normal",
"nutrition" : "",
"sleep" : ""
}
},
"Workout" : {
"26-1-2017" : {
"Bench Press" : {
"exercise" : "Bench Press",
"reps0" : "5",
"weight0" : "185"
}
},
"4-1-2017" : {
"Bench Press" : {
"exercise" : "Bench Press",
"reps0" : "3",
"weight0" : "3"
},
"Push Ups" : {
"exercise" : "Push Ups",
"reps0" : "3",
"weight0" : "3"
}
},
"6-1-2017" : {
"Bench Press" : {
"exercise" : "Bench Press",
"reps0" : "",
"reps1" : "",
"reps2" : "",
"reps3" : "",
"reps4" : "",
"reps5" : "",
"reps6" : "",
"weight0" : "",
"weight1" : "",
"weight2" : "",
"weight3" : "",
"weight4" : "",
"weight5" : "",
"weight6" : ""
}
}
}
},
"exercises" : {
"benchPress" : {
"description" : "bench",
"image" : "pic",
"label" : "Bench Press",
"name" : "Bench Press"
},
"pushUps" : {
"description" : "pushups",
"image" : "pic",
"label" : "Push Ups",
"name" : "Push Ups"
},
"sitUps" : {
"description" : "situps",
"image" : "pic",
"label" : "Sit Ups",
"name" : "Sit Ups"
}
}
}