【问题标题】:Not insert but upsert if document is not found如果找不到文档,则不插入但插入
【发布时间】:2016-01-16 14:06:12
【问题描述】:

我需要检查公司是否没有添加名称插入公司名称,否则更新公司名称。在我的项目中,注册后的用户可以完整地完成个人资料。当用户填写“公司名称”输入并单击保存按钮时,公司名称应添加到公司列表中,如果用户更改公司名称,也应从公司列表中更改。这是我的代码 用户表单html:

 <template name="userFormEdit">
  <form class="form new-company">
    <div class="form-group">
    <label for="companyName">Comapany Name</label>
    <input type="text" class="form-control" name="companyName" id="companyName"     placeholder="" value="{{companyName}}">
    </div>
  </form
</template>


<div class="container-fluid text-center bg-grey">
        <h2>Portfolio</h2>
        <h4>What we have created</h4>
        <div class="row text-center">
            {{ #each companyProfile }}
            {{> companyView }}
            {{/each }}
        </div>
    </div>
</template>
<template name="companyView">
    <div class="col-sm-4">
        <div class="thumbnail">
            <img src="..." alt="Paris">
            <p><strong>{{companyName}}</strong></p>
            <p>Yes, we built Paris</p>
        </div>
    </div>
</template>

这是 .js 文件:

currentUserProfile = new Meteor.Collection('userprofile');
//In client
Template.userFormEdit.events({
        'submit .new-company' : function(event){
            var companyNameVar = event.target.companyName.value;
            currentUserProfile.update(this._id,{$set: {companyName: companyNameVar}});
        }
    });

Template.homepage.helpers({
        companyProfile: function(){
            return currentUserProfile.find();
        }
    });

【问题讨论】:

  • 我认为这个检查在服务器端使用 SQL 查询更好更容易完成。用户单击“保存”按钮,将表单数据发送到服务器,并且您的 php(或其他)脚本将公司名称插入或替换到数据库中。
  • @Banzay 使用 Meteor,在服务器(nodejs)上使用 Javascritp,在数据库中使用 Mongo No-SQL,check it out 它是一个很酷的框架 =D
  • 不确定我是否理解这个问题。你不能只使用upsert 而不是更新吗? “两个事件”是什么意思?

标签: javascript validation meteor input


【解决方案1】:

Meteor 已内置 upsert 用于收藏。你可以用那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 2017-02-10
    • 2018-09-17
    • 2016-04-10
    • 1970-01-01
    • 2015-09-17
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多