0

Googleデータストアからエンティティデータを設定して取得する方法がわかりません。私はさまざまな例を見つけて、どちらが正しいかを理解していません。以下は私がこれまで行ってきたことです。このエラーが発生し続ける:ReferenceError: datastore is not definedデータストアAPIを正しく呼び出さないのですか?ノードを使用してGoogle Datastoreでエンティティデータを設定および受信する方法

server.jsファイル:

var express = require('express'); 
var app = express(); 

const Datastore = require('@google-cloud/datastore'); 
const datastore = Datastore(); 

require('./routes/main')(app); 

require('@google/cloud-debug').start({ 
    keyFilename: './jarvis-hd-live-842e4f78479e.json' 
}); 

const PORT = process.env.PORT || 8080; 

app.listen(PORT,() => { 
    console.log(`Your app is listening on port ${PORT}`); 
}); 

ルートファイル:

app.post('/message', function (request, response) { 
    message = request.body.Body; 

    response.send("<Response><Message>Heyyo!</Message></Response>"); 

    const key = datastore.key('timestamp'); 

    datastore.save({ 
    key: key, 
    data : { 
     timestamp_value: 0 
    } 
    }); 


    datastore.insert(entity) 
     .then(()=> { 
      console.log("Data object inserted successfully."); 
     }); 

}); 

答えて

1

データストアSDKが適切な資格情報を使用して初期化/認証されていません。

server.jsがあるべき -

同じ行に
var express = require('express'); 
var app = express(); 
//The key file is required in a variable. The variable is now a JSON object with the credentials. 
var credentials = require('./jarvis-hd-live-842e4f78479e.json'); 
const Datastore = require('@google-cloud/datastore'); 
//Initialise the datastore object with the proper project id and credentials. 
const datastore = Datastore({ 
    projectId: "your_project_id_goes_here", 
    credentials: credentials 
}); 
/* 
* If you wish to give the filename instead of the credentials object while initialising, replace the credential key with keyFileName and the value with the file name of the key file. 
* Note: The key file should be in the same directory. If another directory, please provide absolute path. 
*/ 
require('./routes/main')(app); 
require('@google/cloud-debug').start({ 
    credentials: credentials 
}); 
const PORT = process.env.PORT || 8080; 

app.listen(PORT,() => { 
    console.log(`Your app is listening on port ${PORT}`); 
}); 

、routes.jsは、次のようになります -

app.post('/message', function (request, response) { 
    message = request.body.Body; 

response.send("<Response><Message>Heyyo!</Message></Response>"); 

//While getting a key for an object, you need to mention the namespace on which your kind resides.(Kind can be thought of as a table.) 
const key = datastore.key({ 
    namespace: "your_namespace_goes_here", 
    path: ["kind_name_goes_here","explicit_name/id_of_the_object"] 
}); 
/* 
* If the name/id is kept null in the path key, datastore will assign a numeric id to the key. 
* The name should be set if the application uses some kind of custom naming/identification scheme using UUID/GUID. 
*/ 
datastore.save({ 
    key: key, 
    data : { 
     timestamp_value: 0 
    } 
}); 
datastore.insert(entity) 
    .then(()=> { 
     console.log("Data object inserted successfully."); 
    }); 
}); 
+0

をありがとう!これについての指示はどこで見つかりましたか? –

+0

私はいつもhttps://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/0.5.1/datastoreのドキュメントを参照しています。 私はこの文書が現時点では少しばっちりしていることを知っています。 –

+0

ありがとう!ドキュメンテーションには非常に多くの異なる例がありました。これは多くの助けとなりました。 –

関連する問題