0
我試圖做一個/ post請求到我的服務器,使用Hapi.js給出數據(保存到數據庫(knex))。/post request包含數據使用request.payload(hapi.js)
隨着發佈請求,我試圖使用2個函數,將一個隨機數變成一個十六進制字符串,並將其設置爲我的數據對象中的hexString鍵。
routes.js
module.exports.postData = {
method: 'POST',
path: '/',
handler: (request, reply) => {
const post = request.payload
const data = {
id: uuid.v4(),
timeOut: null,
uri: post.uri,
payload: post.payload,
hexString: undefined
}
store.link.createRandomInt()
.then((randomNum) => {
store.link.createHexString(randomNum)
.then((hex) => {
data.hexString = hex
reply(store.link.createLink(data)).code(201)
})
})
}
}
functions.js
module.exports.createRandomInt = function() {
return new Promise(function (resolve, reject) {
var randomNumber = Math.floor((Math.random() * 100000000))
resolve(randomNumber)
})
}
module.exports.createHexString = function (int) {
return new Promise(function (resolve, reject) {
var hexString = int.toString(32)
resolve(hexString)
})
}
dbFunctions.js
module.exports = {
createLink: function (link) {
return db('links').insert(link)
.then((id) => {
return {
id: uuid.v4(),
timeOut: null,
uri: link.uri,
payload: link.payload,
hexString: link.hexString
}
})
}
}
後curl命令:
curl http://localhost:8000/ -d "uri: 'this is the uri' payload: { jsonObj: 'enter payload info here'}"
什麼帖子curl命令返回:
{"id":"87c467dd-3703-4f9f-a1ac-78551dc33109","timeOut":null}
當我CONSOLE.LOG()返回的數據對象:
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
感謝任何輸入〜
的數據庫建立連結承諾似乎並未被正確解析,我們可以看到完整的代碼? –