【问题标题】:getting issue in update query in sails js在sails js中的更新查询中遇到问题
【发布时间】:2015-08-17 09:33:06
【问题描述】:

我是 Sails js 的新手,遇到了一些问题,我的更新查询显示更新成功,但它没有更新 mongodb 集合中的记录。我不知道我在做什么愚蠢的错误。

这里是控制器功能

edit: function(req, res){
    var uid = req.params.id;
    console.log("Request body----------" +uid);

    console.log(req.body.heading);
    console.log(req.body.message);
    Dashboard.update(
    { _id : uid },
    {title : "req.body.heading" , description : "req.body.message" }).exec(function(err, doc){
    if(err){
        console.log("Error while update"+err);
    }else{
        console.log("Successfuly Updated");
        res.redirect('dashboard/index');
    }
    });     
}

这是模型文件:

module.exports = {

  attributes: {
    title:{
         type:'string',
         //required:true,
         columnName:'title'
    },
    description: {
         type:'text',
         columnName:'description'
    },
    posted_by: {
        type: 'integer',
        columnName: 'posted_by'
    }

  }

控制台消息显示此

Successfully Updated

任何帮助将不胜感激 提前谢谢你

【问题讨论】:

  • 您是否首先检查了具有给定uid 的文档是否存在?此外,如果您指的是 req.body.headingreq.body.message 的值,则不应引用它们。尝试在回调中打印 doc 以查看是否有任何更新。
  • 如果我们打印 doc ,那么它会在控制台中显示此消息
    []成功更新
  • 这只是意味着没有文件被更新。验证具有提供的 id 的文档是否存在。
  • 不,我检查了它存在于 db 中的 id
  • 好的,你能试试用id代替_id吗?

标签: javascript node.js sails.js sails-mongo


【解决方案1】:

使用id 而不是_id 喜欢

Dashboard.update(
  { id : uid },
  {title : "req.body.heading" , description : "req.body.message" }
)

这是Waterline 约定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2017-01-20
    相关资源
    最近更新 更多