couchdb 设计视图,更新文档创建字段
创建/更新文档时是否可以动态更新沙发或更改字段?例如,在设计视图中... validate_doc_update:
function(newDoc, oldDoc, userCtx) {
}
在该函数中,我可以抛出如下错误:
if(!newDoc.user_email && !newDoc.user_name && !newDoc.user_password){
throw({forbidden : 'all fields required'});
}
我的问题是如何重新分配字段?我尝试了这个:
newDoc.user_password ="changed";
更改为一些新值或哈希值。我的总体目标是用node和couchdb构建一个用户注册/登录系统,但还没有找到很好的例子。
Is it possible to have couch update or change fields on the fly when you create/update a doc? For example in the design view.... validate_doc_update:
function(newDoc, oldDoc, userCtx) {
}
Within that function I can throw errors like:
if(!newDoc.user_email && !newDoc.user_name && !newDoc.user_password){
throw({forbidden : 'all fields required'});
}
My Question is how would I reassign a field? I tried this:
newDoc.user_password ="changed";
with changed being some new value or hashed value. My overall goal is to build a user registration/login system with node and couchdb and have not found very good examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
validate_doc_update
函数不能有任何副作用,并且不能在存储之前更改文档。它仅有权阻止更新或允许更新通过。这很重要,因为该函数不仅在用户请求更新时调用,而且在将更改从一个 CouchDB 实例复制到另一个实例时也会调用。因此,对于一个文档,可以多次调用该函数。但是,CouchDB 现在支持文档更新处理程序,可以修改文档甚至从头开始构建文档。这些可用于将非 JSON 输入数据转换为可用文档。您可以在 CouchDB Wiki 中找到一些文档。
在构建自己的用户注册/登录系统之前,我建议您研究一下内置的 CouchDB 安全功能(如果您还没有 - 一些信息 此处)。它们可能对您来说还不够(例如,如果您需要电子邮件验证或类似的东西),但也许您可以在它们的基础上进行构建。
The
validate_doc_update
function cannot have any side effects and cannot change the document before storage. It only has the power to block an update or to let it through. This is important, because the function is not only called when a user requests an update, but also when changes are replicated from one CouchDB instance to another. So the function can be called multiple times for one document.However, CouchDB now supports Document Update Handlers that can modify a document or even build it from scratch. These can be used to convert non-JSON input data into usable documents. You can find some documentation in the CouchDB Wiki.
Before you build your own user registration/login system, I'd suggest you look into the built-in CouchDB security features (if you haven't - some information here). They might not be enough for you (e.g. if you need email validation or something similar), but maybe you can build on them.