简体   繁体   中英

how can i put variable in pouchDb design document

I am creating a design document in pouchDB like the following

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

I am having the error

pouchdb.min.js:7 ReferenceError: param is not defined
  1. Why is param not evaluated ?
  2. How can i evaluate it ?

here is the full function

 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
  });
}

the _id is evaluated fine, just the map function was causing the error

I know this is an old question, but I just had the same problem and solved it like this:

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

Instead of using .toString() just wrap the function in quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM