0
我想上載一個包含多個對象的數組的文件,並用它來替換我的貓鼬數據庫中的表。這是文件的樣子:上傳文件以替換數據庫中的表的問題
var newRooms = [
{name: "yogaStudio",
title: "Yoga Studio",
description: "This is where StarFleet officers go to get bendy",
roomExits: ['kitchen', 'foyer'],}
,
{name: "kitchen",
title: "Kitchen",
description: "You must be starving, please come in and make yourself a sandwich",
roomExits: ['yogaStudio', 'drawingroom'],}
,
{name: "foyer",
title: "Foyer",
description: "This is the entrance hallway",
roomExits: ['yogaStudio', 'kitchen'],},
{name: "drawingroom",
title: "Drawing Room",
description: "Please grab a drink and take a seat, it's stressful navigating around this place",
roomExits: ['kitchen', 'foyer'],}
,
];
這是我使用的上傳和更換功能:
var restore = function(req, res) {
fs.readFile(req.files.roomFile.path, 'utf8', function (err, data) {
if (err) {
res.send("File upload error.");
} else {
overwriteDB(JSON.parse(data), res);
}
});
}
var overwriteDB = function(newDB, res) {
if (!Array.isArray(newDB)) {
res.send("Error: Data structure isn't an array.");
return;
}
// Should validate newDB further before deleting old documents.
Room.remove({}, function(err) {
if (err) {
res.send("Error: Couldn't delete all existing documents" +
"on restore.");
return;
} else {
console.log("All documents removed before restore.");
}
newDB.forEach(function(theRoomtoImport) {
var theRoom = new Room(theRoomtoImport);
theRoom.save(function(err, theRoom) {
if (err) {
console.log("Failure restoring " + theRoom.name);
} else {
console.log("Success restoring " + theRoom.name);
}
});
});
res.send("Restore in progress!");
});
}
但我發現了這個錯誤
[email protected]:~/comp2406/adventure-ajax-demo$ node app.js
Express server listening on port 3000
undefined:1
var newRooms = [
^
SyntaxError: Unexpected token v
at Object.parse (native)
at /home/sarah/comp2406/adventure-ajax-demo/routes/index.js:75:23
at fs.js:252:14
at Object.oncomplete (fs.js:93:15)
任何人都可以幫我解決這個問題?
謝謝!這是棘手的一切保持直線 我是否像這樣結束];要不就 ]? – Sarah
只是]你可以使用基於Web的json驗證器來檢查你的工作 – gmcerveny