我正在尝试将一些代码从回调更改为promise,将.promise添加到aws调用中。
...
<select [(ngModel)]="district_id" name="districts" class="rounded-inputs20 select-select col-md-3">
<option *ngFor="let district of districts" [ngValue]="district.id">{{district.name}}</option>
</select>
...
我认为这是正确的,但测试有问题。我收到错误'TypeError:docClient.scan(...)。promise不是函数'我认为它与模拟的编写方式有关。
const AWS = require("aws-sdk")
const docClient = new AWS.DynamoDB.DocumentClient({
apiVersion: "2012-08-10",
region: process.env.AWS_REGION
})
class Stuff {
getRawItems() {
let params = {
TableName : "TableName"
}
return docClient.scan(params).promise()
.then(function(data) {
return data.Items
})
.catch(function(err) {
console.warn("Error with Dynamo request", err)
throw err
})
}
}
答案 0 :(得分:0)
模拟是问题,这是以下示例代码:
const fakePromise = {
promise : jest.fn()
}
const fakeDynamo = { scan: () => {
return fakePromise
}}
const realAWS = require("aws-sdk")
realAWS.DynamoDB.DocumentClient = jest.fn( () => fakeDynamo )
const Stuff = require("../src/stuff").Stuff
test("Test that the scan is performed and the data comes back", done => {
fakePromise.promise.mockImplementation(() => Promise.resolve({Items:[1,2,3]}))
const stuff = new Stuff()
const defaultItems = stuff.getRawItems(lat, lon)
defaultItems.then ( (data) => {
expect(fakePromise.promise).toHaveBeenCalledTimes(1)
expect(data.length).toEqual(3)
done()
})
})
答案 1 :(得分:0)
我遇到了这个问题并找到了最简单的解决方案,让模拟器发送一个被视为承诺的回调。
var socket = io.connect('http://localhost:3000');
答案 2 :(得分:-1)
也许读过Promises,我想
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="jquery.js"></script>
<script type="text/javascript">
(function() {
window.onload = function() {
var map;
var locations = [
<?php
//konfgurasi koneksi database
mysql_connect('localhost','root','');
mysql_select_db('candralab-map');
$sql_lokasi="select idlokasi,lat,lon
from lokasi ";
$result=mysql_query($sql_lokasi);
// ambil nama,lat dan lon dari table lokasi
while($data=mysql_fetch_object($result)){
?>
['<?=$data->idlokasi;?>', <?=$data->lat;?>, <?=$data->lon;?>],
<?
}
?>
];
var options = {
zoom: 12, //level zoom
//posisi tengah peta
center: new google.maps.LatLng(-7.8008, 110.380643),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('peta'), options);
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map,
icon: 'icon.png'
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
var id= locations[i][0];
$.ajax({
url : "get_info.php",
data : "id=" +id ,
success : function(data) {
$("#info").html(data);
}
});
}
})(marker, i));
}
};
})();</script>
应该是:
return docClient.scan(params).promise()
.then(function(data) {
return data.Items
})
.catch(function(err) {
console.warn("Error with Dynamo request", err)
throw err
})
解析器告诉你: return docClient.scan(params)
.then(function(data) {
return data.Items
})
.catch(function(err) {
console.warn("Error with Dynamo request", err)
throw err
})
函数的结果没有promise
函数。