我正在尝试创建一个正文并将我的网址发送到存储我的 content
对象的后端。但是,当我使用 const body = JSON.stringify(content);
时,它不会读取属性file
。
这是我的服务代码:
addMessage(content: Content) {
console.log("Service:", content); // File property is in the content object I sent
const body = JSON.stringify(content); // File property missing here
console.log("Body:", body);
const headers = new Headers({'Content-Type': 'application/json'});
const token = localStorage.getItem('token')
? '?token=' + localStorage.getItem('token')
: '';
return this.http.post('http://localhost:3000/content' + token, body, {headers: headers})
.map((response: Response) => {
const result = response.json();
const newContent = new Content(
result.obj.name,
result.obj.user.firstName,
result.obj._id,
result.obj.user._id,
);
this.contents.push(newContent);
return newContent;
})
.catch((error: Response) => {
this.errorService.handleError(error.json());
return Observable.throw(error.json());
});
}
这是我的控制台:
您可以看到该文件出现在内容中,但没有出现在正文中。我尝试执行 JSON.parse(JSON.stringify(content));
,令我惊讶的是,content.file
未定义。知道可能出了什么问题吗?
谢谢!
最佳答案
您尝试过 JSON.stringify(JSON.parse(content));
吗?
首先,您需要解析
JSON
,解析后将其字符串化
。
如果对您有帮助,请告诉我! :D
关于javascript - typescript : unable to read a property of an object even though it's there,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43286966/