【问题标题】:MongoDB insert and upsert not working properly - using meteor frame workMongoDB insert 和 upsert 无法正常工作 - 使用流星框架工作
【发布时间】:2016-11-19 06:00:23
【问题描述】:

使用 Meteor 框架版本 1.2.1 我有一个将数据添加到 Mongo 集合的表单。但是我发现javascript代码执行没有错误(并且所有调试console.log消息都按我的预期出现)但集合仍然没有我刚刚插入的数据。问题是这种情况是随机发生的。所以有时插入工作正常,有时插入不起作用。 upsert 命令的类似问题。我的 html 和 js 代码粘贴在下面(注意我没有删除自动发布和不安全的包)。无法弄清楚我的代码中的问题是什么,需要帮助

JS代码

ProjectList = new Mongo.Collection("projectList");
ListItem    = new Mongo.Collection("listItem");
if (Meteor.isClient) {
Template.create.events({
  'submit .add_chapter': function (event) {
    var addtoWhat;
    var chapName = event.target.chapName.value;
    if (event.target.addToWhat.value) {
      if (event.target.addToWhat.value.length > 0) {
        addtoWhat = event.target.addToWhat.value;
      } else {
      }
    } else {
    } 
    if (addtoWhat) {
      var addToWhatItem = ListItem.findOne({name:addtoWhat});
      var item = {
        name : chapName,
        list : [],
      }
      var id = ListItem.insert(item);
      if (id) {
        addToWhatItem.list.push(id);
        if (!ListItem.upsert({_id:addToWhatItem._id}, addToWhatItem))  {
          alert("Unable to upsert");
        }
      } else {
        alert("Unable to insert");
      }

    } else {
      var projList;
      if (!ProjectList.findOne({projectId:"123"})) {
        projList = {
          projectId : "123",
          list : [],
        };
        var topid = ProjectList.insert(projList);
        if (topid) {
          console.log ("Top Insert succesful with id " + topid);
        } else {
          console.log ("Error Top Insert unsuccesful with id ");
        }
      }
      projList = ProjectList.findOne({projectId:"123"});
      var item = {
        name : chapName,
        list : [],
      }
      var id = ListItem.insert(item);
      projList.list.push(id);
      if(!ProjectList.upsert({_id:projList._id}, projList)) {
        console.log("Upsert failed");
      } else {
        console.log("Upsert successful");
      }
    }
  }, 
  'submit .gen_tree' : function(event) {
     getElements();
  }

});

function getElements() {
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

HTML 代码

         <head>
           <title>test_hierarchy2</title>
         </head>

         <body>
           <h1>Welcome to Meteor!</h1>
           {{> create}}
         </body>

         <template name="create">
           <form class="add_chapter" method="post">
             <label>addToWhat</label>
             <input type="text" name="addToWhat">
             <label>chapName</label>
             <input type="text" name="chapName">
             <button type="submit">Add</button>
           </form>
           <form class="gen_tree" method="post">
              <button type="submit">generate</button>
           </form> 
         </template>

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    试试这个

    ProjectList.update({_id:projList._id}, projList, {upsert:true}, function(error,doc){
       if(error){
           console.log(error);
       }
       else{
           console.log(doc);
       }
    })
    

    【讨论】:

    • 这没有帮助。但是我希望控制台上会打印一些消息,无论是否有错误,但没有收到任何消息。
    • 还有一件事。当集合正确更新时,回调有一些打印。
    • 那么成功了吗??控制台上打印的是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2015-10-08
    • 2019-08-30
    • 2018-03-10
    • 1970-01-01
    • 2023-03-29
    • 2023-03-13
    相关资源
    最近更新 更多