node.js - 通过 Express 持续更新 Backbone 模型

标签 node.js backbone.js express sails.js waterline

我正在尝试更新客户端集合中表示为模型的数据和 mongo db 集合中的文档。事件触发执行此操作的方法是单击 View 上的元素。 客户端是一个 Backbone 应用程序。

在服务器端,我使用带有 Express 的 Node 和带有 Waterline ORM 的 Mongodb。对于此请求,我使用:

app.put('/posts/:id', function(req, res){
app.models.posts.update( req.params.id, function(err, result){
    if(err) return res.status(500).json({ err: err });
    res.json( result );
});

});

View 中的事件方法是:

    updatePost: function(e){
    e.preventDefault();
    //update the new content of the fields on the server.
        //find model to update in the collection by colleciton.findWhere.
    this.modelid = $(e.currentTarget).attr('id');
    this.modeltoupdate = this.collection.findWhere( { id: this.modelid } );
        //change model attributes as needed by model.set().
    this.modeltoupdate.set(
        {
            title: $('#title').text(),
            body: $('#body').text(),
        });
        //save the model on server with wait:true to wait server aknowledge, and add to colelction adn rerender view.
    this.modeltoupdate.save(
        {
            wait:true,
            success: function(){
                this.collection.add( this.modeltoupdate );
                this.render();
                this.renderPost();
            },
        }
    );
},

此 View 的模板是:

<script type="text/template" id="postTemplate">
        <a href="/">All Posts</a>

        <p id='author'>{{author}}</p>
        <p id='title' contenteditable='false'>{{title}}</p>
        <p id='createdat'>{{createdAt}}</p>
        <p id='body' contenteditable='false'>{{body}}</p>
        <a id='{{id}}' class='editpost' href=''>Edit this post</a>
        <a id='{{id}}' class='updatepost' href=''>Update this post</a>

        <a href="/">All Posts</a>
    </script>

但我在 Safari 检查器的资源列表中看到始终加载带有旋转图标的资源。单击它并检查与其相关的请求和响应,显示请求是模型的属性,并按预期更新了字段,但响应显示加载 gif,没有响应。

内容可编辑属性没有问题,当单击“更新此帖子”链接时,它们都设置为 true。

这是我创建的服务器端路由、req params 或 req body 吗? BB 将它们发送到目标 /posts/548e00e61e96a70d0fa4ad50,因此 /posts/:id,这似乎我的 app.put() url 是正确的。

最佳答案

问题出在 app.put() 函数中的服务器端代码上。代码片段调用此函数时缺少强制参数,未向其提供第二个参数。第二个参数是要放入现有模型中的新值,该模型由搜索条件(第一个参数)选择。

app.put('/posts/:id', function(req, res){
  app.models.posts.update( {id: req.params.id}, req.body, function(err, result){
    if(err) return res.status(500).json({ err: err });
    res.json( result );
  });
});

或者仅更新更改的值:

app.put('/posts/:id', function(req, res){
  var changedmodel = {
    id: req.params.id,
    title: req.body.title,
    body: req.body.body
  }; 
  app.models.posts.update( {id: req.params.id}, changedmodel, function(err, result){
    if(err) return res.status(500).json({ err: err });
    res.json( result );
  });
});

关于node.js - 通过 Express 持续更新 Backbone 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513831/

相关文章:

jquery - 重新排列 Marionette 复合 View 中的模型索引

javascript - fetch() 未调用其成功和错误回调函数 (backbone.js)

node.js - 如果 mongodb 类型是字符串,查询仅返回文档

node.js - 如何制作基于角色的中间件以在nodejs和mongoose中以适当的权限访问api?

android - Windows 和 Android 应用程序是否需要 SSL?

javascript - 如何在javascript中比较两个时间戳值?

node.js - 将流写入缓冲区对象

sql - node.js 登录无法使用 azure sql 数据库

model-view-controller - Mvc前端和Mvc后端: wrong architecture?

node.js - 异步查询到nodeJS中的数据库