【问题标题】:how can i put variable in pouchDb design document如何将变量放入 pouchDb 设计文档中
【发布时间】:2016-12-31 15:23:17
【问题描述】:

我正在 pouchDB 中创建一个设计文档,如下所示

var desingDoc = {
    _id: '_design/' + param,
    views: {
      by_name: {
        map: function (doc) {
          if (doc._id.indexOf(param + '_') == 0)
            emit(doc._id);
        }.toString()
      }
    }
  };

我遇到了错误

pouchdb.min.js:7 ReferenceError: param is not defined
  1. 为什么不评估参数?
  2. 我该如何评估它?

这里是完整的功能

 function index(param) {
  var desingDoc = {
    _id: '_design/' + param,
    views: {
      by_name: {
        map: function (doc) {
          if (doc._id.indexOf(param + '_') == 0)
            emit(doc._id);
        }.toString()
      }
    }
  };

  _db.put(desingDoc).then(function () {
    console.log('success')
  }).catch(function (err) {
    console.log(err)
    // todo handle the non saved case
  });
}

_id 的评估结果很好,只是 map 函数导致了错误

【问题讨论】:

  • 引用的错误是因为您还没有定义param。能贴出完整代码吗?
  • param 是函数参数。
  • 您尝试插入数据库的地图应该是我认为的值,您将其转换为字符串。为此,您已经调用了为 map 编写的函数。

标签: javascript angularjs pouchdb


【解决方案1】:

我知道这是一个老问题,但我只是遇到了同样的问题并像这样解决了它:

map: "function (doc) { if (doc._id.indexOf(" + param + "_) == 0) { emit(doc._id); } }"

不要使用 .toString() 而是将函数用引号括起来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2012-05-05
    • 2017-10-28
    • 2016-05-07
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多