{error:' not_found',原因:'已删除',名称:' not_found',状态:404,消息:'已删除' }

时间:2017-01-31 07:56:11

标签: javascript view couchdb

我正在构建像这样的CouchDB设计文档:

const ddoc={
    "_id":"_design/index2nd",
    "views":{
        "by_id":{
            "map": (function(doc){emit(doc._id, doc);}).toString()
        },
        "by_username":{
            "map": (function(doc){emit(doc.username, doc);}).toString()
        }
    }
}

我用这个保存设计文档:

db.put(ddoc).then(()=>{
    ////done!
}).catch(err=>{
    console.log('error: design doc is not saved: ', err)
})

在您查询索引之前,索引尚未构建,因此我执行空查询以启动新版本:

db.query("index2nd/by_id",{
    limit:0 // don't return any results
}).then(result=>{
    // index was built!
}).catch(err=>{
    console.log('error: index2nd/by_id is not built: ', err)
})

db.query("index2nd/by_username",{
    limit:0//don't return any results
}).then(result=>{
    //index is built
}).catch(err=>{
    console.log('error: index2nd/by_username is not built: ', err)
})

但是我收到以下错误:

  

错误:未构建index2nd / by_id:{error:' not_found',     理由:'已删除',     名称:' not_found',     状态:404,     消息:'已删除' }

     

错误:未构建index2nd / by_username:{error:' not_found',     理由:'已删除',     名称:' not_found',     状态:404,     消息:'已删除' }

1 个答案:

答案 0 :(得分:0)

最后,我使用pouchdb-upsert插件解决了这个问题。请参阅thisthat

const ddoc={
    "_id":"_design/myIndex",
    "views":{
        "by_id":{
            "map": (function (doc){emit(doc._id);}).toString()
        },
        "by_username":{
            "map": (function (doc){emit(doc.username);}).toString()
        }
    }
}

PouchDB.plugin(require('pouchdb-upsert'))

const diffFunc=doc=>{
    return ddoc;//always return the same ddoc
}

db.upsert('_design/myIndex',diffFunc).then(res=>{
    // happily done
}).catch(err=>{
    console.log('error @ upsert: ', err)
})