在云代码中解析查询后保留对象
Retaining objects after parse query in cloud code
我正在编写一些从第三方 API 拉取 JSON 的 Parse Cloud 代码。我想修改它,检查它是否已经存在,如果不存在,请保存它。检查对象是否存在后,我无法保留该对象。
这是我正在尝试做的一个例子。当我到达成功块时,我需要原始汽车对象,以便我可以将它保存到解析数据库中。虽然它是未定义的。我是 JS 的新手,可能在这里遗漏了一些明显的东西。
for (var j = 0, leng = cars.length; j < leng; ++j) {
var car = cars[j];
var Car = Parse.Object.extend("Car");
var query = new Parse.Query(Car);
query.equalTo("dodge", car.model);
query.find({
success: function(results) {
if (results.length === 0) {
//save car... but here car is undefined.
}
},
error: function(error) {
console.error("Error: " + error.code + " " + error.message);
}
});
}
如果有人能指出正确的方向,我将不胜感激。谢谢!
您的函数 returns 在查找方法 returns 之前。这就是js的异步特性。使用异步库中的 async.parrallel 之类的东西。 http://npm.im/async
更新 20150929:
这里有一些代码可以向您展示我是如何做到的,这是我正在进行的一个副业项目。数据存储在 MongoDB 中,并使用 Mongoose ODM 进行访问。我正在使用异步瀑布,因为我需要下一个方法中异步函数的值……因此在异步库中使用了名称 waterfall
。 :)
async.waterfall([
// Get the topics less than or equal to the time now in utc using the moment time lib
function (done) {
Topic.find({nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
done(err, topics);
});
},
// Find user associated with each topic
function (topics, done) {
// Loop over all the topics and find the user, create a moment, save it,
// and then send the email.
// Creating moment (not the moment lib), save, sending email is done in the
// processTopic() method. (not showng)
async.each(topics, function (topic, eachCallback) {
processTopic(topic, eachCallback);
}, function (err, success) {
done(err, success);
}
);
}
// Waterfall callback, executed when EVERYTHING is done
], function (err, results) {
if(err) {
log.info('Failure!\n' + err)
finish(1); // fin w/ error
} else {
log.info("Success! Done dispatching moments.");
finish(0); // fin w/ success
}
});
一旦你习惯了承诺,它们就会真正简化你的生活。这是使用承诺的更新或创建模式的示例...
function updateOrCreateCar(model, carJSON) {
var query = new Parse.Query("Car");
query.equalTo("model", model);
return query.first().then(function(car) {
// if not found, create one...
if (!car) {
car = new Car();
car.set("model", model);
}
// Here, update car with info from carJSON. Depending on
// the match between the json and your parse model, there
// may be a shortcut using the backbone extension
car.set("someAttribute", carJSON.someAttribute);
return (car.isNew())? car.save() : Parse.Promise.as(car);
});
}
// call it like this
var promises = [];
for (var j = 0, leng = carsJSON.length; j < leng; ++j) {
var carJSON = carsJSON[j];
var model = carJSON.model;
promises.push(updateOrCreateCar(model, carJSON));
}
Parse.Promise.when(promises).then(function() {
// new or updated cars are in arguments
console.log(JSON.stringify(arguments));
}, function(error) {
console.error("Error: " + error.code + " " + error.message);
});
我正在编写一些从第三方 API 拉取 JSON 的 Parse Cloud 代码。我想修改它,检查它是否已经存在,如果不存在,请保存它。检查对象是否存在后,我无法保留该对象。
这是我正在尝试做的一个例子。当我到达成功块时,我需要原始汽车对象,以便我可以将它保存到解析数据库中。虽然它是未定义的。我是 JS 的新手,可能在这里遗漏了一些明显的东西。
for (var j = 0, leng = cars.length; j < leng; ++j) {
var car = cars[j];
var Car = Parse.Object.extend("Car");
var query = new Parse.Query(Car);
query.equalTo("dodge", car.model);
query.find({
success: function(results) {
if (results.length === 0) {
//save car... but here car is undefined.
}
},
error: function(error) {
console.error("Error: " + error.code + " " + error.message);
}
});
}
如果有人能指出正确的方向,我将不胜感激。谢谢!
您的函数 returns 在查找方法 returns 之前。这就是js的异步特性。使用异步库中的 async.parrallel 之类的东西。 http://npm.im/async
更新 20150929:
这里有一些代码可以向您展示我是如何做到的,这是我正在进行的一个副业项目。数据存储在 MongoDB 中,并使用 Mongoose ODM 进行访问。我正在使用异步瀑布,因为我需要下一个方法中异步函数的值……因此在异步库中使用了名称 waterfall
。 :)
async.waterfall([
// Get the topics less than or equal to the time now in utc using the moment time lib
function (done) {
Topic.find({nextNotificationDate: {$lte: moment().utc()}}, function (err, topics) {
done(err, topics);
});
},
// Find user associated with each topic
function (topics, done) {
// Loop over all the topics and find the user, create a moment, save it,
// and then send the email.
// Creating moment (not the moment lib), save, sending email is done in the
// processTopic() method. (not showng)
async.each(topics, function (topic, eachCallback) {
processTopic(topic, eachCallback);
}, function (err, success) {
done(err, success);
}
);
}
// Waterfall callback, executed when EVERYTHING is done
], function (err, results) {
if(err) {
log.info('Failure!\n' + err)
finish(1); // fin w/ error
} else {
log.info("Success! Done dispatching moments.");
finish(0); // fin w/ success
}
});
一旦你习惯了承诺,它们就会真正简化你的生活。这是使用承诺的更新或创建模式的示例...
function updateOrCreateCar(model, carJSON) {
var query = new Parse.Query("Car");
query.equalTo("model", model);
return query.first().then(function(car) {
// if not found, create one...
if (!car) {
car = new Car();
car.set("model", model);
}
// Here, update car with info from carJSON. Depending on
// the match between the json and your parse model, there
// may be a shortcut using the backbone extension
car.set("someAttribute", carJSON.someAttribute);
return (car.isNew())? car.save() : Parse.Promise.as(car);
});
}
// call it like this
var promises = [];
for (var j = 0, leng = carsJSON.length; j < leng; ++j) {
var carJSON = carsJSON[j];
var model = carJSON.model;
promises.push(updateOrCreateCar(model, carJSON));
}
Parse.Promise.when(promises).then(function() {
// new or updated cars are in arguments
console.log(JSON.stringify(arguments));
}, function(error) {
console.error("Error: " + error.code + " " + error.message);
});