我使用用于javascript的parse sdk连接到我的数据库并插入三条记录。我的代码可以工作,但我经常重复我自己,我想知道是否有一种更聪明、更好的方法可以在不重复代码的情况下进行这些调用并插入数据?这是我的密码:
const Parse = require('parse/node');
Parse.initialize(
"test",
"test"
);
Parse.serverURL = 'url';
const CarObject = Parse.Object.extend("Car");
const firstCar = new CarObject();
const secondCar = new CarObject();
const thirdCar = new CarObject();
firstCar.set("driver", "Sean Plott");
secondCar.set("driver", "Brad Plott");
thirdCar.set("driver", "John Davis");
firstCar.save()
.then((result) => {
// Execute any logic that should take place after the object is saved.
console.info("New object was created with objectId:", result.id);
}).catch((error) => {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error("Error message:", error.message);
});
secondCar.save()
.then((result) => {
// Execute any logic that should take place after the object is saved.
console.info("New object was created with objectId:", result.id);
}).catch((error) => {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error("Error message:", error.message);
});
thirdCar.save()
.then((result) => {
// Execute any logic that should take place after the object is saved.
console.info("New object was created with objectId:", result.id);
}).catch((error) => {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
console.error("Error message:", error.message);
});
3条答案
按热度按时间z18hc3ub1#
我更愿意将数据存储在数组中,并使用下面的帮助函数创建对象:
nszi6y052#
这似乎足够简洁-将回调拉到单独的函数中,并使用
for...of
环tvmytwxo3#
在所有可能的方法中,我想说:
如果您想一辆接一辆地保存所有车辆:
如果您想同时保存所有车辆(速度更快,但会影响数据库,因此只有在没有太多车辆时才使用此选项)