为啥 Falcor 的 setValue() 不适用于外部模型?

Posted

技术标签:

【中文标题】为啥 Falcor 的 setValue() 不适用于外部模型?【英文标题】:Why doesn't Falcor's setValue() work on external model?为什么 Falcor 的 setValue() 不适用于外部模型? 【发布时间】:2015-11-19 17:17:01 【问题描述】:

在本地 Falcor 模型上使用 setValue() 时,它可以按预期工作,但是当模型移动到服务器时,会出现“500(内部服务器错误)”。

下面的例子可以正常运行(脚本在浏览器中运行)

var $ref = falcor.Model.ref;
var model = new falcor.Model(
  cache: 
      productsById: 
           1: 
               name: "Product ABC",
               otherAdd: "something 1"
           ,
           2: 
               name: "Product 123",
               otherAdd: "something 2"
           ,

      ,
      _view: [ $ref('productsById[1]') ],
      _cart: []
  
);

model.
  getValue("_view[0].name").
  then(function(response1)  
    console.log( response1 );
    model.
      setValue("_view[0].name", "Another book").
      subscribe(function(response2)
        console.log( response2 );
      );
  );

但是,当将此设置更改为远程 Falcor 模型时,它会失败。 (脚本在浏览器中运行)

var $ref = falcor.Model.ref;
var model = new falcor.Model(source: new falcor.HttpDataSource('/model.json') );

model.
  getValue("_view[0].name"). // <-- works fine
  then(function(response1)  
    console.log( response1 );
    model.
      setValue("_view[0].name", "Another book"). // <-- fails
      subscribe(function(response2) // <-- fails on both subscribe() and then()
        console.log( response2 );
      );
  );

使用以下节点脚本(脚本在服务器上运行):

var express = require('express');
var app = express();
var falcor = require('falcor');
var falcorExpress = require('falcor-express');
var $ref = falcor.Model.ref;

function example()
    return 
        cache: 
            productsById: 
                 1: 
                     name: "Product ABC",
                     otherAdd: "something 1"
                 ,
                 2: 
                     name: "Product 123",
                     otherAdd: "something 2"
                 ,

            ,
            _view: [ $ref('productsById[1]') ],
            _cart: []
        
    


app.use('/model.json', falcorExpress.dataSourceRoute(function (req, res) 
    return new falcor.
                    Model( example() ).
                    asDataSource();
));

app.use(express.static(__dirname + '/'));
var server = app.listen(8080);

控制台中的错误消息(调用setValue()时):

POST http://localhost:8080/model.json 500 (Internal Server Error)
requestObserver @ falcor.browser.js:9294145.Observable.create.o.subscribe @ falcor.browser.js:9186_subscribe @ falcor.browser.js:2830182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256182.TapObservable.subscribeCore @ falcor.browser.js:16424tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:14304subscribe @ falcor.browser.js:14318182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256182.observableProto.materialize @ falcor.browser.js:16605tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:17513s @ falcor.browser.js:17530182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256182.MapObservable.subscribeCore @ falcor.browser.js:17073tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:14304subscribe @ falcor.browser.js:14318182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256182.MergeAllObservable.subscribeCore @ falcor.browser.js:15858tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:14304subscribe @ falcor.browser.js:14318182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256(anonymous function) @ falcor.browser.js:15419182.Rx.AnonymousObserver.AnonymousObserver.error @ falcor.browser.js:14066182.Rx.internals.AbstractObserver.AbstractObserver.onError @ falcor.browser.js:14000subscribeToSetResponse @ falcor.browser.js:4133182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256(anonymous function) @ falcor.browser.js:15409tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:17513s @ falcor.browser.js:17530182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:1425661.SetResponse.invokeSourceRequest @ falcor.browser.js:4031182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256182.observableProto.finally.observableProto.ensure @ falcor.browser.js:16522tryCatcher @ falcor.browser.js:12847setDisposable @ falcor.browser.js:17513182.ScheduledItem.invokeCore @ falcor.browser.js:13209182.ScheduledItem.invoke @ falcor.browser.js:13197runTrampoline @ falcor.browser.js:13517tryCatcher @ falcor.browser.js:12847scheduleNow @ falcor.browser.js:13528182.Rx.Scheduler.schedulerProto.scheduleWithState @ falcor.browser.js:13250s @ falcor.browser.js:17528182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:1425658.IdempotentResponse.ensureCollect @ falcor.browser.js:3696182.Rx.Observable.observableProto.subscribe.observableProto.forEach @ falcor.browser.js:14256subscribeToResponse @ falcor.browser.js:3956subscribe @ falcor.browser.js:3903(anonymous function) @ test.html:34
test.html:35 Error: Response code 500
    at _handleXhrError (http://netflix.github.io/falcor/build/falcor.browser.js:9316:19)
    at onXhrLoad (http://netflix.github.io/falcor/build/falcor.browser.js:9366:14)
    at XMLHttpRequest.onreadystatechange (http://netflix.github.io/falcor/build/falcor.browser.js:9280:13)

【问题讨论】:

【参考方案1】:

这可能是 express 的问题。 在服务器端:

npm install --save body-parser

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded(extended: true));

【讨论】:

这正是我遇到的问题。如果运行 falcor 的 express 服务器返回 500,请确保正在使用 body-parser。

以上是关于为啥 Falcor 的 setValue() 不适用于外部模型?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 .setValue() 在 for 循环中跳过列?

为啥在可变字典上调用的 setValue:forKeyPath 不会为未知键路径抛出异常?

Swift iOS NSDictionary setValue 崩溃 - 但为啥?

Firebase setValue() 在模拟器上工作,但在设备上不工作,有人知道为啥吗? iOS 10,斯威夫特 3.1

Falcor:如何在 falcor 的 JSON Graph 中定义函数?

Falcor - HTTPDataSource 发布 Json