2015-03-03 26 views
1

我有下面的代碼:JavaScript函數返回的 '男' 或 '未定義'

getLastInsertId = function(collection, field){ 

    field = (typeof field === "undefined") ? '_id' : field; 


    var qry = JSON.parse('{"fields" :{"'+field+'": 1}, "sort" : {"$natural": -1}, "limit": 1}'); 
    //console.log(qry); 
    var lastInsertId = collection.find({}, qry).fetch()[0]; 

    if(lastInsertId === 'undefined'){ 
     //var x = 1; 
     return "1"; 
     //console.log(Number(1)); 


    }else{ 
     //return (+lastInsertId + 1); 

    } 



}; 

如果我使用之後,它返回NaN:

return 1; 

有人可以指導我,我是什麼做錯了事以及如何糾正。

+1

如果(typeof運算lastInsertId === 「未定義」){ }使用 – Alok 2015-03-03 13:19:40

回答

1

試試這個

var qry = {"fields" :{"'+field+'": 1}, "sort" : {"$natural": -1}, "limit": 1}; 
//console.log(qry); 
var lastInsertId = collection.findOne(qry); 
lastInsertId = lastInsertId._id;