解决 "Document must be a JSON object"
Solving "Document must be a JSON object"
我开始通过创建一个使用 couchdb/pouchdb 存储数据的待办事项应用来学习 Ionic/Angular。我创建了一个定义如下的 Todo 模型:
export class TodoModel {
_id : string;
title : string;
description: string;
constructor(_id : string, title: string, description: string){
this._id=_id;
this.title=title;
this.description=description;
}
}
我知道我必须将待办事项对象转换为 JSON 对象,所以我正在尝试这样做:
todoToJson(todo : TodoModel): string{
let todoJson =JSON.stringify(todo);
console.log(typeof(todoJson));
console.log(todoJson);
return todoJson;
}
createTodo(todo : TodoModel) {
this.db.put(this.todoToJson(todo));
}
但是当我实际尝试使用这些功能时,我在控制台中得到了这个:
这是我感到困惑的地方,我的第一个 console.log 告诉我我的对象是一个字符串(Json 是字符串对吗?),第二个的结果是(对我来说) a Json,也就是 correctly interpreted by a json editor,那我做错了什么?
我在上班前发帖很快,我可能会忘记一些细节,所以如果您需要更多信息,请告诉我。
为什么不直接将对象传递给 put 函数?
createTodo(todo : TodoModel) {
this.db.put(todo);
}
我开始通过创建一个使用 couchdb/pouchdb 存储数据的待办事项应用来学习 Ionic/Angular。我创建了一个定义如下的 Todo 模型:
export class TodoModel {
_id : string;
title : string;
description: string;
constructor(_id : string, title: string, description: string){
this._id=_id;
this.title=title;
this.description=description;
}
}
我知道我必须将待办事项对象转换为 JSON 对象,所以我正在尝试这样做:
todoToJson(todo : TodoModel): string{
let todoJson =JSON.stringify(todo);
console.log(typeof(todoJson));
console.log(todoJson);
return todoJson;
}
createTodo(todo : TodoModel) {
this.db.put(this.todoToJson(todo));
}
但是当我实际尝试使用这些功能时,我在控制台中得到了这个:
这是我感到困惑的地方,我的第一个 console.log 告诉我我的对象是一个字符串(Json 是字符串对吗?),第二个的结果是(对我来说) a Json,也就是 correctly interpreted by a json editor,那我做错了什么?
我在上班前发帖很快,我可能会忘记一些细节,所以如果您需要更多信息,请告诉我。
为什么不直接将对象传递给 put 函数?
createTodo(todo : TodoModel) {
this.db.put(todo);
}