2016-08-02 19 views
0

我已經做了一些研究並嘗試了一些東西,比如刪除集合等。沒有任何幫助。在創建貓鼬模式時獲取Object.keys在非對象上調用

代碼:

MongoClient.saveData = function(schemaDefinition, data, collectionName){ 
    console.log("Schema definition: "+schemaDefinition+" collection name: "+collectionName); 
    var RecordSchema = new mongoose.Schema(schemaDefinition);//{ Email: String, FirstName: String});//({any: Schema.Types.Mixed }); 
    console.log("Schema created."); 
    var RecordModel = mongoose.model(collectionName, RecordSchema); 
    console.log("Model created. Inserting in batches.") 
    RecordModel.insertMany(data) 
    .then(function(mongooseDocuments) { 
     console.log("Insertion was successful."); 
    }) 
    .catch(function(err) { 
     console.log("Error while inserting to DB.") 
    }); 

錯誤:

/home/ubuntu/ds_queuesystem/node_modules/mongoose/lib/schema.js:381 
    var keys = Object.keys(obj); 
        ^
TypeError: Object.keys called on non-object 
    at Function.keys (native) 
    at Schema.add (/home/ubuntu/ds_queuesystem/node_modules/mongoose/lib/schema.js:381:21) 
    at new Schema (/home/ubuntu/ds_queuesystem/node_modules/mongoose/lib/schema.js:98:10) 
    at Function.MongoClient.saveData (/home/ubuntu/ds_queuesystem/MongoClient.js:34:21) 
    at /home/ubuntu/ds_queuesystem/DS_QueueSystem.js:84:18 
    at nextTask (/home/ubuntu/ds_queuesystem/node_modules/async/dist/async.js:6627:18) 
    at /home/ubuntu/ds_queuesystem/node_modules/async/dist/async.js:6621:17 
    at /home/ubuntu/ds_queuesystem/node_modules/async/dist/async.js:339:31 
    at /home/ubuntu/ds_queuesystem/node_modules/async/dist/async.js:840:20 
    at /home/ubuntu/ds_queuesystem/DS_QueueSystem.js:143:3 
    at null.<anonymous> (/home/ubuntu/ds_queuesystem/node_modules/csv-parse/lib/index.js:71:16) 
    at EventEmitter.emit (events.js:117:20) 
    at _stream_readable.js:920:16 
    at process._tickCallback (node.js:415:13) 

Schema定義:

var schemaDefinition = "{SCHID: String, Priority: Number, Status: String, Json: Schema.Types.Mixed})"; 
+0

你能解釋一下你的要求嗎?你的問題到底是什麼? – Shrabanee

+0

我只是想執行上面的代碼沒有錯誤。上面的代碼負責創建模式,並將批量記錄添加到集合中。而已。 –

回答

1

schemaDefinition應該是一個Object,而不是文字。

嘗試:

var schemaDefinition = { 
    SCHID: String, 
    Priority: Number, 
    Status: String, 
    Json: Schema.Types.Mixed 
}; 

文檔:http://mongoosejs.com/docs/guide.html

+0

謝謝。有點奇怪的錯誤信息,不是嗎? –