javascript - typescript : unable to read a property of an object even though it's there

标签 javascript json node.js typescript console.log

我正在尝试创建一个正文并将我的网址发送到存储我的 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());
        });
}

这是我的控制台:

enter image description here

您可以看到该文件出现在内容中,但没有出现在正文中。我尝试执行 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/

相关文章:

javascript - Angular 新手: Debugging Typescript Error "All declarations of ' imageLoader' must have identical type parameters"

javascript - 使用文件上传最大大小事件的代码到简单代码

php - 使用 ZF 使 Zend_Rest_Server 返回 JSON 而不是 XML

html - Typescript 获取 JSON 对象

node.js - 如何在循环中正确实现async或await操作-nodejs

javascript - 将更改和其他操作绑定(bind)到 div 内的多个 ajax 加载元素

javascript - 如何使用angularjs获取下拉列表框文本?

json - 黑客新闻中的flutter小部件测试中, 'int'类型不是 'String'类型的子类型

javascript - 如何使用 HTML、CSS 和 postgres 构建简单的应用程序

.net - SQL Server 数据库插入/更新 TCP 通知