完成couchapp 教程l 后,必须完成最后一步:保存创建的比萨饼。
为此,我创建了一个 JS 函数“saveToppings”,该函数被执行(如萤火控制台所示)但在保存我的 JSON 文档时失败,并显示以下消息:
无法保存文档:文档必须是 JSON 对象。
所以我明白,我的文档不是 JSON 文档,但我不知道如何正确执行此操作。
这是“saveToppings”函数的代码:
function(e){
var json_toppings = JSON.stringify($$(this).toppings);
var merged_toppings = "{ \"type\":\"topping\", \"contents\":" + json_toppings + "}";
$.log('json_toppings: '+ json_toppings.toString());
$.log('merged_toppings: '+ merged_toppings.toString());
$$(this).app.db.saveDoc(merged_toppings, {
success : function() {
alert("Doc saved successfully.");
}
});
}
...以及来自控制台的调试:
json_toppings: [{"top":"tomatoes"},{"top":"bacon"},{"top":"cheese"}]
merged_toppings: { "type":"topping", "contents":[{"top":"tomatoes"},{"top":"bacon"},{"top":"cheese"}]}