我第一次使用cloudant-couchdb
而我只是遇到了这个问题。
我试图通过我的node.js服务器动态地将视图插入我的数据库。
这是我的代码。
app.post("/listSections", function(req, res) {
var moduleID = req.body.name;
console.log(moduleID);
db.insert(
{ "views":
{ moduleID: //create view with name = moduleID
{
"map": function (doc) {
//some function
}
}
},
'_design/section', function (error, response) {
console.log("Success!");
});
});
我想动态创建一个视图,视图名称是变量moduleID的值。如何在插入函数中传递该变量?
答案 0 :(得分:1)
无法在对象文字定义中插入变量名称。事先创建对象。
Imports System.Text.RegularExpressions
Public Function getCLM(ediString as string) as string
Dim regex As New Regex("CLM\*(\d*\.?\d*)")
Dim match As Match = regex.Match(yourString)
If match.Success Then Return match.Value
End Function
Dim yourString as String = ""
Dim clmNumber as string = getCLM(yourString)
如果您使用的是ES6且计算属性名称可用,则可以改为:
var obj = {};
obj[moduleID] = {map: function () {}};
db.insert({views: obj},
请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer