私は私のテーブルからの投稿を与え、また、各ポストの著者に関する情報を返すクエリ書いた:MySQLのクエリはJSON_OBJECT(の結果として文字列を返します)
SELECT post.id, post.text, post.datetime, JSON_OBJECT(
'username', user.username,
'firstName', user.firstName,
'firstName', user.lastName) as author
FROM post
INNER JOIN user ON post.authorId = user.id;
をしかし、応答にauthor
フィールドがあります文字列:
CAST(JSON_OBJECT(
'username', user.username,
'firstName', user.firstName,
'firstName', user.lastName) as JSON) as author
:私は
CAST
とにかく
author
を使用していることを修正しようとした
author: "{"username": "@", "firstName": null}"
datetime: "2017-05-02T20:23:23.000Z"
id: 10
text: "5555"
は文字列です0
なぜそれが起こったのですか、それを修正する方法は?
UPD:
私はNode.jsのとExpressを使用してサーバからのデータを送信します。console.log
の
app.get('/posts', (req, res, next) => {
getPosts().then((posts) => {
res.setHeader('Content-Type', 'application/json');
res.send(posts);
})
.catch(next);
});
// ...
getPosts() {
return new Promise((resolve, reject) => {
const query = `
SELECT post.id, post.text, post.datetime, JSON_OBJECT(
'username', user.username,
'firstName', user.firstName,
'firstName', user.lastName) as author
FROM post
INNER JOIN user ON post.authorId = user.id;`;
this.connection.query(query, (err, result) => {
if(err) {
return reject(new Error("An error occured getting the posts: " + err));
}
console.log(result) // prints author as a string
resolve(result || []);
});
});
}
結果:
{
id: 1,
text: 'hello, world!',
datetime: 2017-05-02T15:08:34.000Z,
author: '{"username": "@", "firstName": null}'
}
私もここにしようとしたがres.json(posts)
にres.send(posts)
を変更するが、それは助けにはならない。
投稿をサーバーに触れクライアントからMy機能:
export const getPosts =() => {
customFetch(apiUrl + '/posts')
.then(response => response.json())
.then(json => json)
};
何かをあなたの必要性を排除し
一度に解析します。それは何ですか? – e4c5
私はjavascriptを使用しています – rel1x
私はノードエキスパートではありませんが、私が理解していることは、悲しいことに、あなたが選択したものがjsonオブジェクトをサポートしていないように見えるRDBMSへの接続方法が複数あります。結局のところJSONは最近mysqlに追加されたものです – e4c5