我在一个无法使用的+ lambda + dynamodb环境中工作,我无法得到一个线索。
我的功能如下:
getList : () => {
return new Promise(function(resolve, reject) {
var params = {param1 : value1, param2: value2} //parameters for a DB query
getItemsfromDB(params, function(err, data) { //get all items in a table, returns a Promise
if (err) return reject(err);
data.Items.forEach ((item, index) : => { //Items constain a obejct, with list of objects, like a arraym return another Promise.
getItemPropDetailsfromDB(item.prop1, (itemDetails) : => { //I get item by item details from another table. item.prop1 is string
data.Items[index].prop1 = itemDetails; //replace string with object details
//log says that at this time, itemDetails are the object retrived as I want.
});
})
return resolve(data.Items) // return the initial prop1 string
}
}
我不知道如何修改原始对象,或返回一个新对象。我做了很多测试,但我无法弄清楚我错在哪里 对不起我的英文和我(可能)不完整的信息,我不知道还有什么可以测试。
更新 最后我解决了这个问题:
getList : () => new Promise((resolve, reject) => {
var params = {param1 : value1, param2: value2} //parameters for a DB query
getItemsfromDB(params, (err, data) => {
if (err) return reject(err);
Promise.all(data.Items.map((item, index) => new Promise((resolve) =>
getItemPropDetailsfromDB(item.prop1, (itemDetails) => {
var newItem = data.Items[index];//map() return a new object with modifies properties, so i copy one by one
newItem.prop1 = itemDetails;//replace prop1
return resolve(newItem);//return modified item
})//this return a new object.
))).then((newData) => => {return resolve(newData)}) //then i retrive the new object
})
})
感谢您的帮助!
答案 0 :(得分:0)
你想做的事情似乎很奇怪,应该以其他方式做,但试试这个:
import subprocess,os
import random
def makeDir():
# directly create directory name as a string
tempDir = "/mntDir/{}".format(random.randrange(111111,999999))
# no need for a subprocess, python handles this well!
os.mkdir(tempDir)
# returns the absolute directory name, as string
return tempDir
def mountShare(hostname, username, password):
mountDir = makeDir()
rc = subprocess.call(["mount","-t","cifs", "-o",
"username="+username+",password="+password,
"//"+hostname+"/c$",
mountDir])
if rc!=0:
print("Mounting failed")
另请注意getList : () => new Promise((resolve, reject) => {
var params = {param1 : value1, param2: value2} //parameters for a DB query
getItemsfromDB(params, (err, data) => {
if (err) return reject(err);
Promise.all(data.Items.map((item, index) => new Promise((resolve) =>
getItemPropDetailsfromDB(item.prop1, (itemDetails) => {
data.Items[index].prop1 = itemDetails;
resolve(itemDetails);
})
))).then(() => resolve(data.Items))
})
})
似乎没有使用典型的错误优先方法调用其回调,因此我不知道如果该函数遇到错误将会发生什么(它将调用其回调函数{ {1}}或者它根本不会调用回调?)。