2014-12-07 57 views
2

我試圖更新「地圖」欄,但這樣做導致錯誤的嘗試:如何卡桑德拉其主要更新收集郵戳型

錯誤:

error: database errorTypeError: Not a valid bigint, expected Long/Number/Buffer, obtained '2014-12-07T13:53:10.658Z' 

嘗試

var query_attendance = 'UPDATE emp_attendance SET attendace = attendace + ? where year = ? and month = ? and emp_id = ?'; 
var udpateAttendance = function(empId, timestampMillis, cb){ 
    var foundDate = new Date(timestampMillis); 
    var year = foundDate.getUTCFullYear(); 
    var month = foundDate.getUTCMonth(); 
    var date = foundDate.getUTCDate(); 
    var attendace = {}; 
    attendace['2014-12-07T13:53:10.658Z'] = 'Present'; 
// winston.info('attendace' + JSON.stringify(foundDate)); 
    var values = [attendace, year, month, empId]; 
    var options = { 
     hints: ['map','int','int','int'], 
     prepare: true 
    }; 
    winston.info('Values: ' + JSON.stringify(values)); 
    client.execute(query_attendance, values, options, function(err, resultSet){ 
     winston.info('Query Completed'); 
     if(err){ 
      winston.error('database error' + err); 
      cb(err, null); 
      return;   
     } 
     winston.info('Query successful'); 
     cb(null, resultSet); 
    }); 
} 

筆者認爲:

我想我需要告訴驅動程序,地圖集合中的鍵類型是時間戳類型,但我沒有找到如何指定這樣的輸入驅動程序。

回答

1

經過很多調試到cassandra驅動程序庫後,我終於發現有什麼錯誤。

當'cassandra-driver'中的'encoder.js'接收到類型爲date的輸入時,'encodeTimestamp()'使用'instanceof'關鍵字檢查類型。 這是我的錯誤導致了錯誤的位置,因爲

attendace[foundDate] 
or 
attendace['2014-12-07T13:53:10.658Z'] 

都可能使鍵式「串」的(是js的新手,也無法確認它)。 因此,問題的解決方案是i need to pass date object as key of attendace variable

注:

雖然,我仍然無法傳遞日期對象作爲另一個對象的關鍵,但這是不同的問題完全。 只是爲了繼續運行流程(直到我找到了將日期作爲對象傳遞的方式),我在cassandra驅動程序中對'encoder.js'的'encodeTimestamp()'進行了小小修改。 變化

function encodeTimestamp (value, type) { 
    console.log('Encoding time stamp: ' + value + '\ttype:'+type); 
    //parsing the date object, 
    var convertedDate = new Date(value); 
    // check if supplied value is converted into date, if so, assign its value to value variable and continue with original process 
    if((convertedDate) && (convertedDate!= NaN)){ 
     value = convertedDate; 
    } 
    if (value instanceof Date) { 
     console.log('Value is date '); 
     value = value.getTime(); 
    }else{ 
     console.log('input value ' + value + ' is not a date'); 
    } 
    return encodeBigNumber (value, type); 
    } 

This is hack only,所以我希望,必須有一些真正的解決方案,有些人會拿出的是,

+0

這是不可能的,是的,我已經創建了一個[吉拉票呢](https://datastax-oss.atlassian.net/browse/NODEJS-57) – jorgebg 2014-12-09 09:28:03

+0

只是爲了讓我的工作按時完成,是我解決問題的正確方法,否則會影響其他查詢或API中的過程 – guptakvgaurav 2014-12-09 09:33:19

+0

解決方案是正確的,它不應該影響你的應用程序的其他部分。 – jorgebg 2014-12-09 13:26:54