1

在本地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)
4

1 回答 1

4

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

npm install --save body-parser

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
于 2015-08-27T08:47:33.357 回答