关于ndoe和mysql交互的新知识,我正在尝试获取查询中的collunm数据,到目前为止,我已经做了什么: groupmemberlist是我要获取每个值的循环数组。
%% EDIT %%
我已经添加了这些更改,但仍只能在变量中得到未定义:
for (var m = 0; m < groupmemberlist.length; m++) {
var imgreturned = findpicture(groupmemberlist[m], returnimage);
console.log(imgtag);
}
此循环可以找到以下图片:
function returnimage(imgid) {
imgtag = '<img href="/uploads/' + imgid[0].User_Profile_Photo + ">";
}
function findpicture(id, callback) {
return new Promise(function (resolve, reject) {
db.query('SELECT User_Profile_Photo FROM users WHERE id = ?', [id], function (error, results, fields) {
if (error) {
throw error;
} else {
imgFromId = results;
}
callback(imgFromId);
});
})
}
我得到的错误是(重复4次):
Query {
domain: null,
_events:
{ error: [Function],
packet: [Function],
end: [Function],
timeout: [Function],
'start-tls': [Function] },
_eventsCount: 5,
_maxListeners: undefined,
_callback: [Function],
_callSite: Error
at Protocol._enqueue (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Connection.query (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\Connection.js:200:25)
at findpicture (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\routes\index.js:210:31)
at Query.<anonymous> (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\routes\index.js:232:35)
at Query.<anonymous> (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\Connection.js:502:10)
at Query._callback (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\Connection.js:468:16)
at Query.Sequence.end (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\protocol\sequences\Query.js:139:8)
at Query.EofPacket (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\protocol\sequences\Query.js:123:8)
at Protocol._parsePacket (C:\Users\yvyma.DESKTOP-74AK6UQ\Desktop\Nodeapp\express-cc\node_modules\mysql\lib\protocol\Protocol.js:278:23),
_ended: false,
_timeout: undefined,
_timer: Timer { _object: [Circular], _timeout: null },
sql: 'SELECT User_Profile_Photo FROM users WHERE id = 6 ',
values: [ 6 ],
typeCast: true,
nestTables: false,
_resultSet: null,
_results: [],
_fields: [],
_index: 0,
_loadError: null,
_connection:
Connection {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
config:
ConnectionConfig {
host: '127.0.0.1',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: 'root',
password: undefined,
database: 'express-cc',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
ssl: false,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631,
protocol41: true },
_socket:
Socket {
connecting: false,
_hadError: false,
_handle: [Object],
_parent: null,
_host: null,
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: false,
_bytesDispatched: 151,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
_idleTimeout: -1,
_idleNext: null,
_idlePrev: null,
_idleStart: 9257,
_destroyed: false,
[Symbol(asyncId)]: 44,
[Symbol(bytesRead)]: 0,
[Symbol(asyncId)]: 47,
[Symbol(triggerAsyncId)]: 10 },
_protocol:
Protocol {
domain: null,
_events: [Object],
_eventsCount: 6,
_maxListeners: undefined,
readable: true,
writable: true,
_config: [Object],
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshake: true,
_handshaked: true,
_ended: false,
_destroyed: false,
_queue: [Array],
_handshakeInitializationPacket: [Object],
_parser: [Object] },
_connectCalled: true,
state: 'authenticated',
threadId: 767 } }
我还尝试在查询函数中说var imgname =(results [0] .User_Profile_Photo),然后尝试console.log记录var imgname,但是它返回var undefined的错误。
答案 0 :(得分:0)
您的代码杂乱无章。您有些地方不对劲。这是一个更好的版本,虽然不完美:
Sub AccImport()
Dim acc As New Access.Application
Dim myValue As Variant
myValue = InputBox("Enter table name for access export")
acc.OpenCurrentDatabase "C:\Users\User 1\Documents\Database21.accdb"
acc.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=myValue, _
Filename:=Application.ActiveWorkbook.FullName, _
HasFieldNames:=True, _
Range:="Sheet2$A1:AL104"
CurrentDb.TableDefs(myValue).Fields("F4").Properties!ColumnWidth = 2500
CurrentDb.TableDefs(myValue).Fields("F7").Properties!ColumnWidth = 2500
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
myValue = Empty
MsgBox " The data has been exported"
Application.DisplayAlerts = False
Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.Delete
Range("A1").Select
Application.DisplayAlerts = True
End Sub
现在,让我们解释一下您的代码有什么问题。它会为您提供一些很好的概念来理解。
CurrentDb.TableDefs(myValue).Fields("F4").Properties!ColumnWidth = 2500
正在使用回调函数。这意味着它是一个function findpicture(id) {
let imgFromId;
db.query('SELECT User_Profile_Photo FROM users WHERE id = ?', [id], function (error, results, fields) {
if (error) {
throw error;
}
imgFromId = results;
console.log(imgfromid);
});
}
请求。这意味着结果需要一些时间才能返回。现在,通过将db.query
分配给asyncronous
来尝试做的事情不是获得查询结果,而是分配对从db.query
返回的内容的引用。
在我共享的更正版本中,您需要在回调函数之外定义imgFromId
。返回结果后,将结果分配给回调函数中的结果。
但是,这仍然不能解决您的问题。由于您要处理异步代码,因此需要查看Promises和async / await。