LoginSignup
0
0

More than 3 years have passed since last update.

Node.js: CouchDB のデータを更新 (Update)

Posted at
couch_update.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  couch_update.js
//
//                  Jul/27/2020
// ---------------------------------------------------------------
var Client = require('node-rest-client').Client
// ---------------------------------------------------------------
function get_current_date_proc()
{
    const today = new Date ()
    var ddx = (1900 + today.getYear ()) + "-" + (today.getMonth () +1)
    ddx += "-" + today.getDate ()

    return ddx
}

// ---------------------------------------------------------------
console.error("*** 開始 ***")

const key_in=process.argv[2]
const population_in=process.argv[3]

console.log(key_in)
console.log(population_in)

const url = "http://localhost:5984/nagano/" + key_in

var client = new Client()
client.get(url, function(data, response) {
    console.log(data)
    console.log(data._rev)
    console.log(data.name)

    const date_mod = get_current_date_proc()
    console.log(date_mod)

    var unit_aa = new Object ()
    unit_aa["population"] = population_in
    unit_aa["name"] = data.name
    unit_aa["date_mod"] = date_mod
    const json_str = JSON.stringify (unit_aa)
    console.log(json_str)

    const url_put = url + '?rev=' + data._rev

    var args = {
        data: json_str,
        headers: { "Content-Type": "application/json" }
        }

    client.put(url_put, args, function(data, response) {
        console.log(data)
        console.error("*** 終了 ***")
        })

    })

// ---------------------------------------------------------------

実行

export NODE_PATH=/usr/lib/node_modules
./couch_update.js t2023 9345100
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0