更新流星上的MongoDB收藏
update mongodb collection on meteor
我有一个Meteor应用程序来列出名称和号码,我可以插入和删除名称和数字,现在我正在尝试添加一个按钮来更改名称或数字字段如何在流星中做到这一点
这是我的代码
。.html
<head>
<title>Meteor_CRUD</title>
</head>
<body>
{{> index}}
</body>
<template name="index">
<h1>List</h1>
<table>
<thead>
<td>Name</td>
<td>Number</td>
</thead>
<tbody>
<tr>
<td><input class="name" type="text"/></td>
<td><input class="number" type="text"/></td>
<td><button class="add">add</button></td>
</tr>
{{#each list}}
<tr>
<td>{{name}}</td>
<td>{{number}}</td>
<td><button class="del">del</button></td>
<td><button class="change">change</button></td>
</tr>
{{/each}}
</tbody>
</table>
。.js
List = new Meteor.Collection("list");
if (Meteor.isClient) {
Template.index.helpers({
list : function () {
return List.find();
}
});
Template.index.events({
'click .del' : function (evt, tmpl) {
List.remove(this._id);
},
'click .add' : function (evt, tmpl) {
var name = tmpl.find(".name").value;
var number = tmpl.find(".number").value;
List.insert({name: name, number: number});
tmpl.find(".name").value = '';
tmpl.find(".number").value = '';
},
'click .change' : function (evt, tmpl) {
//List.update(this._id);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}
有人可以帮助我吗?
谢谢
如果要
更新字段,首先,使用某个字段(此处为_id)查找文档,然后将更改推送到文档中,如下所示
'click .change' : function (evt, tmpl) {
//List.update(this._id);
var name=this.name;
var number=this.number;
List.update({_id:this._id},{$push:{name:name,number:number}});
}
通过在服务器端插入以下代码,确保您有权更新集合
List.allow({
update:function(userId, doc, fields, modifier)
{
//anyone can update the collection
//you can write some filters to restrict the updating to only owner of the document
return true;
}
});
相关文章:
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- 如何处理node.js节点mongodb中的连接和查询队列
- MongoDB (php) - 以数组而不是多个属性的形式返回文档属性
- 使用mongodb更新中的一个变量
- 键名中的 MongoDB 点 (.)
- 用于视频类型的MongoDB结构's
- Mongodb数千个并行运行的查询
- 使用HTML5 localStorage和jQuery添加到收藏夹/书签
- 如何访问对象内部的“categoryIds”字段/键,该对象包含mongodb's `ObjectId(s)`数
- 无法在Meteor/MongoDB中返回当前用户对象
- 检索MongoDB binData并显示为<img>src
- Mongodb/JS:查找最短(最早)日期
- 如何在MongoDB中将String转换为Array
- NodeJS expressJS使用multer和passport将文件上传并保存到MongoDB
- MongoDB + SQL Select equivalent
- mongodb regex parameter
- 在玉石模板中显示Mongodb收藏
- Meteor.js/MongoDB:查找文档's在收藏中的位置
- 更新流星上的MongoDB收藏
- 使用node和mongoDB创建具有可共享url的收藏列表