gpt4 book ai didi

javascript - SailsJS/水线 ORM : Update multiple entries using only one query

转载 作者:行者123 更新时间:2023-12-02 23:48:50 26 4
gpt4 key购买 nike

我目前正在使用 SailsJS 框架,并且我正在尝试仅使用 MongoDB 数据库中的一个查询来更新一组元素。

这是我正在做的事情,但记录没有更新..

传入 JSON:

{
"cars": [
{
"id": "5cb5cbd5c395a01b4c9d86da",
"latitude": "-5",
"longitude": "551"
},
{
"id": "5cb5cbd7c395a01b4c9d86db",
"latitude": "-4",
"longitude": "4421",
}
]
}

Controller :

async setLocationOfCars(req, res) {

try {

sails.log.info(controllerName + "::" + req.options.action + "() - called");

const carsLocationArray = req.body.cars;

let response = CarService.setLocationOfCars(carsLocationArray);
switch (response.status) {
case constants.RESOURCE_SUCCESSFULLY_UPDATED :
return HttpResponseService.json(200, res, constants.RESOURCE_SUCCESSFULLY_UPDATED, response.data);
default:
return HttpResponseService.internalServerError(res, response);
}

} catch(err) {
return HttpResponseService.internalServerError(res, err);
}

}

服务:

async setLocationOfCars(cars) {

try {

const arrayOfIdsUpdated = [];
_.forEach(cars, async function(car){

let attributesToUpdate = {};
if (car.hasOwnProperty("latitude")) {
attributesToUpdate.latitude = car.latitude;
}
if (car.hasOwnProperty("longitude")) {
attributesToUpdate.longitude = car.longitude;
}

await Car.updateOne({
id: car.id
}).set(attributesToUpdate);

arrayOfIdsUpdated.push(gateway.id)

});

return {
status: constants.RESOURCE_SUCCESSFULLY_UPDATED,
data : arrayOfIdsUpdated
};

} catch (err) {
return {
status : constants.DATABASE_ERROR,
name : err.name ? err.name : "",
message: err.message ? err.message: "",
stack : err.stack ? err.stack : "",
code : err.code ? err.code : "",
};
}

}

最佳答案

在你的 Controller 中

确保您正在等待服务的响应。

let response = await CarService.setLocationOfCars(carsLocationArray);

在您的“服务”中

我可能会将 _.forEach 替换为常规的 for 循环。在您的情况下,如下所示,确保实际定义了 gateway.id 以及您想要返回给调用代码的值(您的问题中没有引用 gateway )。

for (let car of cars) {
let attributesToUpdate = {};
if (car.hasOwnProperty("latitude")) {
attributesToUpdate.latitude = car.latitude;
}
if (car.hasOwnProperty("longitude")) {
attributesToUpdate.longitude = car.longitude;
}

await Car.updateOne({
id: car.id
}).set(attributesToUpdate);


// Where does gateway.id come from?
arrayOfIdsUpdated.push(gateway.id)
}

也就是说,这将执行 cars.length 数量的数据库查询,而不是“仅使用一个查询”。

但是为什么不使用 _.forEach 呢?

在 forEach 中使用 async 回调看起来不会做您可能希望它做的事情。在任何对 updateOne 的调用运行之前,您的 arrayOfIdsUpdated 可能会返回到空的 Controller 代码。

以此为例:

const _ = require('lodash');

function doNothing() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 0)
});
}

var foos = [1, 2, 3, 4, 5, 6]
_.forEach(foos, async (foo) => {
await doNothing();
console.log(`Finished #${foo}`);
});


console.log('Done!')

运行它会给出输出

Done!
Finished #1
Finished #2
Finished #3
Finished #4
Finished #5
Finished #6

注意“完成!”是如何实现的。在 forEach 回调中等待响应之前被记录到控制台。

关于javascript - SailsJS/水线 ORM : Update multiple entries using only one query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55728339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com