意外结束JSON输入错误

时间:2018-04-26 17:23:47

标签: reactjs api

我有一个reactjs页面,它使用了一个API: ...通过postman(GET请求)进行测试时,会显示以下结果:

...

class DisplayFileSystemModel : public QFileSystemModel {
    Q_OBJECT
    Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
public:
    ...    
    Q_INVOKABLE QModelIndex setRootPath(const QString &newPath){
       QModelIndex ix =  QFileSystemModel::setRootPath(newPath);
       setRootIndex(ix);
       return ix;
    }
    QModelIndex rootIndex() const{
        return mRootIndex;
    }
    void setRootIndex(const QModelIndex &rootIndex){
        if(mRootIndex == rootIndex)
            return;
        mRootIndex = rootIndex;
        Q_EMIT rootIndexChanged();
    }
    Q_SIGNAL void rootIndexChanged();
private:
    QModelIndex mRootIndex;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    qmlRegisterUncreatableType<DisplayFileSystemModel>("io.qt.examples.quick.controls.filesystembrowser", 1, 0,
                                                       "FileSystemModel", "Cannot create a FileSystemModel instance.");
    DisplayFileSystemModel *fsm = new DisplayFileSystemModel(&engine); // change
    fsm->setRootPath(QDir::homePath());
    fsm->setResolveSymlinks(true);
    engine.rootContext()->setContextProperty("fileSystemModel", fsm);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

#include "main.moc"

这是我的代码:

    ...
    Row {
        ...

        Repeater {
            model: [ "rootPath", "None", "Single", "Extended", "Multi", "Contig."]
            Button {
                text: modelData
                exclusiveGroup: eg
                checkable: modelData != "rootPath"
                checked: index === 1
                onClicked: {
                    if(modelData != "rootPath")
                        view.selectionMode = index
                    else{
                        view.model.setRootPath("/home/myusername/test/")
                    }
                }
            }
        }
    }
...    
TreeView {
    id: view
    anchors.fill: parent
    anchors.margins: 2 * 12 + row.height
    model: fileSystemModel
    rootIndex: fileSystemModel.rootIndex //change
    selection: sel
...

...错误发生在上面代码的这一行: .then(response =&gt; response.json())

我可以得到一些关于我做错了什么的帮助吗?

1 个答案:

答案 0 :(得分:1)

<强>解释

  1. 您正在使用.then()两次。
  2. 无法调用
  3. response.json(),因为您从端点返回JSON对象。如果内部有任何回调函数,则无法在其上调用函数.json()
  4. 使用setState()功能设置状态就足够了。
  5. <强>解决方案

    componentDidMount() {
      fetch(this.state.url).then(response => {
        this.setState({ response });
      });
    }